Skip to content

Commit

Permalink
refactor(index): emit errors using @fastify/error
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoPatroclo committed Oct 20, 2024
1 parent cfc977e commit a7fca0a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
const fp = require('fastify-plugin')
const Ajv = require('ajv')
const AjvCore = require('ajv/dist/core')
const createError = require('@fastify/error')

const ReplyValidationFailError = createError('FST_RESPONSE_VALIDATION_FAILED_VALIDATION', '%s', 500)
const NoSchemaDefinedError = createError('FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED', 'No schema defined for %s')

function fastifyResponseValidation (fastify, opts, next) {
let ajv
Expand Down Expand Up @@ -70,7 +74,7 @@ function fastifyResponseValidation (fastify, opts, next) {
let validate = statusCodes[reply.statusCode] || statusCodes[(reply.statusCode + '')[0] + 'xx']

if (responseStatusCodeValidation && validate === undefined) {
next(new Error(`No schema defined for status code ${reply.statusCode}`))
next(new NoSchemaDefinedError(`status code ${reply.statusCode}`))
return
}

Expand All @@ -79,17 +83,17 @@ function fastifyResponseValidation (fastify, opts, next) {
if (validate.constructor === Object) {
const mediaName = reply.getHeader('content-type').split(';', 1)[0]
if (validate[mediaName] == null) {
next(new Error(`No schema defined for media type ${mediaName}`))
next(new NoSchemaDefinedError(`media type ${mediaName}`))
return
}
validate = validate[mediaName]
}

const valid = validate(payload)
if (!valid) {
const err = new Error(schemaErrorsText(validate.errors))
const err = new ReplyValidationFailError(schemaErrorsText(validate.errors))
err.validation = validate.errors
reply.code(500)
reply.code(err.statusCode)
return next(err)
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test('Should return a validation error', async t => {
t.assert.strictEqual(response.statusCode, 500)
const data = response.json()
t.assert.deepStrictEqual(data, {
code: 'FST_RESPONSE_VALIDATION_FAILED_VALIDATION',
statusCode: 500,
error: 'Internal Server Error',
message: 'response/answer must be number'
Expand Down Expand Up @@ -66,6 +67,7 @@ test('Should support shortcut schema syntax', async t => {

t.assert.strictEqual(response.statusCode, 500)
t.assert.deepStrictEqual(JSON.parse(response.payload), {
code: 'FST_RESPONSE_VALIDATION_FAILED_VALIDATION',
statusCode: 500,
error: 'Internal Server Error',
message: 'response/answer must be number'
Expand Down Expand Up @@ -139,6 +141,7 @@ test('Should check media types', async t => {

t.assert.strictEqual(response.statusCode, 500)
t.assert.deepStrictEqual(JSON.parse(response.payload), {
code: 'FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED',
statusCode: 500,
error: 'Internal Server Error',
message: 'No schema defined for media type application/not+json'
Expand Down Expand Up @@ -373,6 +376,7 @@ test('Enable response status code validation for a specific route', async t => {

t.assert.strictEqual(response.statusCode, 500)
t.assert.deepStrictEqual(JSON.parse(response.payload), {
code: 'FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED',
statusCode: 500,
error: 'Internal Server Error',
message: 'No schema defined for status code 200'
Expand Down Expand Up @@ -408,6 +412,7 @@ test('Enable response status code validation for every route', async t => {

t.assert.strictEqual(response.statusCode, 500)
t.assert.deepStrictEqual(JSON.parse(response.payload), {
code: 'FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED',
statusCode: 500,
error: 'Internal Server Error',
message: 'No schema defined for status code 200'
Expand Down

0 comments on commit a7fca0a

Please sign in to comment.