From 8520a6f11a95cf3bf4fbeda8c20abfa28f91d8a1 Mon Sep 17 00:00:00 2001 From: Dwayne Harris Date: Sat, 21 Sep 2019 13:04:14 -0400 Subject: [PATCH] WIP --- src/plugins/api/index.ts | 20 +++++++++++++++----- src/schemas.ts | 16 ++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/plugins/api/index.ts b/src/plugins/api/index.ts index 1593958..5e98a09 100644 --- a/src/plugins/api/index.ts +++ b/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 = 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) diff --git a/src/schemas.ts b/src/schemas.ts index 3a82eb4..6bb0b51 100644 --- a/src/schemas.ts +++ b/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' }, + }, + }, + }, + }, }