Dwayne Harris 5 years ago
parent
commit
8520a6f11a
  1. 20
      src/plugins/api/index.ts
  2. 16
      src/schemas.ts

20
src/plugins/api/index.ts

@ -1,5 +1,6 @@
import { CosmosClient } from '@azure/cosmos'
import { Plugin } from 'fastify'
import trim from 'lodash/trim'
import { Server, IncomingMessage, ServerResponse } from 'http'
import { JWT } from '../../lib/crypto'
@ -11,7 +12,7 @@ import posts from './posts'
import uploads from './uploads'
import users from './users'
import { PluginOptions } from '../../types'
import { PluginOptions, HttpError } from '../../types'
interface Database {
client: CosmosClient
@ -60,11 +61,20 @@ const plugin: Plugin<Server, IncomingMessage, ServerResponse, PluginOptions> = a
})
server.setErrorHandler(function(error, request, reply) {
request.log.error('Error: %s', error.message)
reply.send({
const response: HttpError = {
message: error.message,
})
}
if (error.validation) {
response.errors = error.validation.map(e => {
return {
field: trim(e.dataPath, '.'),
message: e.message,
}
})
}
reply.send(response)
})
server.register(authentication)

16
src/schemas.ts

@ -76,8 +76,16 @@ export const groupListingSchema: JSONSchema = {
export const errorSchema: JSONSchema = {
type: 'object',
properties: {
statusCode: { type: 'number' },
error: { type: 'string' },
field: { type: 'string' },
}
message: { type: 'string' },
errors: {
type: 'array',
items: {
type: 'object',
properties: {
field: { type: 'string' },
message: { type: 'string' },
},
},
},
},
}
Loading…
Cancel
Save