Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I handle errors in my custom assessment code
    WordPress SEO


How do I handle errors in my custom assessment code


Here are some tips for handling errors in your custom assessment code for the Yoast SEO plugin:

1. Wrap your code in a try-catch block: Ensure your `getResult()` method is wrapped in a try-catch block to handle any unexpected errors that may occur during the assessment.

javascript
getResult(paper, researcher, i18n) {
  try {
    // Your assessment code here
  } catch (e) {
    // Handle the error
    const assessmentResult = new AssessmentResult();
    assessmentResult.setScore(0);
    assessmentResult.setText(i18n.sprintf(
      i18n.dgettext(
        'js-text-analysis',
        'An error occurred in the \'%1$s\' assessment: %2$s'
      ),
      'your-assessment-name',
      e.toString()
    ));
    return assessmentResult;
  }
}

2. Log the error: In the catch block, you can log the error to help with debugging. You can use the `console.error()` method to log the error message and stack trace.

javascript
getResult(paper, researcher, i18n) {
  try {
    // Your assessment code here
  } catch (e) {
    console.error('Error in custom assessment:', e);
    // Handle the error
    // ...
  }
}

3. Provide a fallback assessment result: If an error occurs, you can provide a fallback assessment result with a low score and an error message to inform the user about the issue.

javascript
getResult(paper, researcher, i18n) {
  try {
    // Your assessment code here
  } catch (e) {
    console.error('Error in custom assessment:', e);
    const assessmentResult = new AssessmentResult();
    assessmentResult.setScore(0);
    assessmentResult.setText(i18n.sprintf(
      i18n.dgettext(
        'js-text-analysis',
        'An error occurred in the \'%1$s\' assessment: %2$s'
      ),
      'your-assessment-name',
      e.toString()
    ));
    return assessmentResult;
  }
}

4. Handle specific errors: If you anticipate certain types of errors, you can handle them separately and provide more specific feedback. For example, if your assessment relies on a specific data structure, you can check for that and handle the error accordingly.

javascript
getResult(paper, researcher, i18n) {
  try {
    // Your assessment code here
    if (!paper.hasCustomData) {
      const assessmentResult = new AssessmentResult();
      assessmentResult.setScore(0);
      assessmentResult.setText(i18n.sprintf(
        i18n.dgettext(
          'js-text-analysis',
          'The \'%1$s\' assessment requires custom data that is not available.'
        ),
        'your-assessment-name'
      ));
      return assessmentResult;
    }
  } catch (e) {
    console.error('Error in custom assessment:', e);
    const assessmentResult = new AssessmentResult();
    assessmentResult.setScore(0);
    assessmentResult.setText(i18n.sprintf(
      i18n.dgettext(
        'js-text-analysis',
        'An error occurred in the \'%1$s\' assessment: %2$s'
      ),
      'your-assessment-name',
      e.toString()
    ));
    return assessmentResult;
  }
}

By following these practices, you can ensure that your custom assessment code gracefully handles errors and provides meaningful feedback to the user when issues occur.

Citations:
[1] https://developer.yoast.com/customization/yoast-seo/adding-custom-assessments/
[2] https://github.com/Yoast/wordpress-seo/issues/5075
[3] https://developer.yoast.com/customization/yoast-seo/adding-custom-data-analysis/
[4] https://yoast.com/help/xml-sitemap-errors/
[5] https://wordpress.org/support/topic/enable-seo-controls-and-assessments-breaks-block-editor-on-some-pages/