Repository: freeCodeCamp/freeCodeCamp
File: api/src/utils/error-formatting.ts (TypeScript)

Code before:
};

/**
 * Format validation errors for /project-completed.
 *
 * @param errors An array of validation errors.
 * @param message Unqiue message for the function.
 * @returns Formatted errors that can be used in the response.
 */
export const formatProjectCompletedValidation = (
  errors: ErrorObject[],
  message?: FormattedError['message']
): FormattedError => {
  const error = getError(errors);

  return error.instancePath === '' &&
    error.params.missingProperty === 'solution'
    ? {
        type: 'error',
        message:
          'You have not provided the valid links for us to inspect your work.'
      }
    : {
        type: 'error',
        message:
          message ?? 'That does not appear to be a valid challenge submission.'
      };
};

/**
 * Format validation errors for /coderoad-challenge-completed.
 *
 * @param errors An array of validation errors.
 * @returns Formatted errors that can be used in the response.
 */
export const formatCoderoadChallengeCompletedValidation = (
  errors: ErrorObject[]
): CodeRoadError => {
  const error = getError(errors);

  // TODO(Post-MVP): Return error saying that the body is not an object.
  if (error.instancePath === '' && error.message === 'must be object')
    return { type: 'error', msg: `'tutorialId' not found in request body` };

  if (
    error.instancePath === '' &&
    error.params.missingProperty === 'coderoad-user-token'
  ) {
    return {
      type: 'error',
      msg: `'Coderoad-User-Token' not found in request headers`

Review comment (suggestion):
```suggestion
        message: 'That does not appear to be a valid challenge submission.'
```

Code after:
};

/**
 * Format validation errors for /project-completed.
 *
 * @param errors An array of validation errors.
 * @returns Formatted errors that can be used in the response.
 */
export const formatProjectCompletedValidation = (
  errors: ErrorObject[]
): FormattedError => {
  const error = getError(errors);

  return error.instancePath === '' &&
    error.params.missingProperty === 'solution'
    ? {
        type: 'error',
        message:
          'You have not provided the valid links for us to inspect your work.'
      }
    : {
        type: 'error',
        message: 'That does not appear to be a valid challenge submission.'
      };
};

/**
 * Format validation errors for /coderoad-challenge-completed.
 *
 * @param errors An array of validation errors.
 * @returns Formatted errors that can be used in the response.
 */
export const formatCoderoadChallengeCompletedValidation = (
  errors: ErrorObject[]
): CodeRoadError => {
  const error = getError(errors);

  // TODO(Post-MVP): Return error saying that the body is not an object.
  if (error.instancePath === '' && error.message === 'must be object')
    return { type: 'error', msg: `'tutorialId' not found in request body` };

  if (
    error.instancePath === '' &&
    error.params.missingProperty === 'coderoad-user-token'
  ) {
    return {
      type: 'error',
      msg: `'Coderoad-User-Token' not found in request headers`
    };
  } else {
    // by process of elimination: