diff --git a/api/src/routes/certificate.test.ts b/api/src/routes/certificate.test.ts index 46380bf0100..31f7734ea81 100644 --- a/api/src/routes/certificate.test.ts +++ b/api/src/routes/certificate.test.ts @@ -627,6 +627,25 @@ describe('certificate routes', () => { }); expect(response.status).toBe(200); }); + + test('should return cert-not-found if there is no cert with that slug', async () => { + const response = await superRequest( + '/certificate/showCert/foobar/not-a-valid-cert-slug', + { + method: 'GET' + } + ); + expect(response.body).toEqual({ + messages: [ + { + type: 'info', + message: 'flash.cert-not-found', + variables: { certSlug: 'not-a-valid-cert-slug' } + } + ] + }); + expect(response.status).toBe(404); + }); }); }); }); diff --git a/api/src/routes/certificate.ts b/api/src/routes/certificate.ts index 4208923b517..d5e19425e8b 100644 --- a/api/src/routes/certificate.ts +++ b/api/src/routes/certificate.ts @@ -19,7 +19,6 @@ import { } from '../../../shared/config/certification-settings'; import { normalizeChallenges, removeNulls } from '../utils/normalize'; import { SHOW_UPCOMING_CHANGES } from '../utils/env'; -import { formatCertificationValidation } from '../utils/error-formatting'; const { legacyFrontEndChallengeId, @@ -287,15 +286,7 @@ export const unprotectedCertificateRoutes: FastifyPluginCallbackTypebox = ( fastify.get( '/certificate/showCert/:username/:certSlug', { - schema: schemas.certSlug, - errorHandler(error, request, reply) { - if (error.validation) { - void reply.code(400); - return formatCertificationValidation(error.validation); - } else { - fastify.errorHandler(error, request, reply); - } - } + schema: schemas.certSlug }, async (req, reply) => { try { @@ -307,9 +298,13 @@ export const unprotectedCertificateRoutes: FastifyPluginCallbackTypebox = ( if (!isKnownCertSlug(certSlug)) { void reply.code(404); return reply.send({ - type: 'info', - message: 'flash.cert-not-found', - variables: { certSlug } + messages: [ + { + type: 'info', + message: 'flash.cert-not-found', + variables: { certSlug } + } + ] }); } diff --git a/api/src/schemas/certificate/cert-slug.ts b/api/src/schemas/certificate/cert-slug.ts index cf76746402c..f413ed6ab10 100644 --- a/api/src/schemas/certificate/cert-slug.ts +++ b/api/src/schemas/certificate/cert-slug.ts @@ -108,16 +108,16 @@ export const certSlug = { ) }) ]), - 400: Type.Object({ - type: Type.Literal('error'), - message: Type.String() - }), 404: Type.Object({ - message: Type.Literal('flash.cert-not-found'), - type: Type.Literal('info'), - variables: Type.Object({ - certSlug: Type.String() - }) + messages: Type.Array( + Type.Object({ + message: Type.Literal('flash.cert-not-found'), + type: Type.Literal('info'), + variables: Type.Object({ + certSlug: Type.String() + }) + }) + ) }), 500: Type.Object({ type: Type.Literal('danger'), diff --git a/api/src/utils/error-formatting.ts b/api/src/utils/error-formatting.ts index 72b5253301c..43a05b3e5f2 100644 --- a/api/src/utils/error-formatting.ts +++ b/api/src/utils/error-formatting.ts @@ -1,7 +1,4 @@ import { ErrorObject } from 'ajv'; -import { certTypes } from '../../../shared/config/certification-settings'; - -type CertLogs = (typeof certTypes)[keyof typeof certTypes]; type FormattedError = { type: 'error'; @@ -50,30 +47,6 @@ export const formatProjectCompletedValidation = ( }; }; -/** - * 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 formatCertificationValidation = ( - errors: ErrorObject[] -): FormattedError => { - const error = getError(errors); - - return error.instancePath === '' && - Object.values(certTypes).includes(error.params.missingProperty as CertLogs) - ? ({ - type: 'error', - message: - 'You have not provided the valid param for us to display the certification.' - } as const) - : ({ - type: 'error', - message: 'That does not appear to be a valid certification request.' - } as const); -}; - /** * Format validation errors for /coderoad-challenge-completed. *