diff --git a/packages/schema/src/resolver.ts b/packages/schema/src/resolver.ts index e891ef7a2a..b94888019a 100644 --- a/packages/schema/src/resolver.ts +++ b/packages/schema/src/resolver.ts @@ -1,5 +1,4 @@ import { BadRequest } from '@feathersjs/errors'; -import { Schema } from './schema'; export type PropertyResolver = ( value: V|undefined, @@ -13,7 +12,9 @@ export type PropertyResolverMap = { } export interface ResolverConfig { - schema?: Schema, + // TODO this should be `Schema` but has recently produced an error, see + // https://github.com/ThomasAribart/json-schema-to-ts/issues/53 + schema?: any, validate?: 'before'|'after'|false, properties: PropertyResolverMap } @@ -94,7 +95,9 @@ export class Resolver { })); if (hasErrors) { - throw new BadRequest(`Error resolving data ${status?.properties.join('.')}`, errors); + const propertyName = status?.properties ? ` ${status.properties.join('.')}` : ''; + + throw new BadRequest('Error resolving data' + propertyName, errors); } return schema && validate === 'after' diff --git a/packages/schema/test/fixture.ts b/packages/schema/test/fixture.ts index 2e0b340224..cbebc0bfa2 100644 --- a/packages/schema/test/fixture.ts +++ b/packages/schema/test/fixture.ts @@ -2,6 +2,7 @@ import { feathers, HookContext, Application as FeathersApplication } from '@feathersjs/feathers'; import { memory, Service } from '@feathersjs/memory'; +import { GeneralError } from '@feathersjs/errors'; import { schema, resolve, Infer, resolveResult, @@ -86,6 +87,10 @@ export const messageResultResolver = resolve { const { userId } = message; + if (context.params.error === true) { + throw new GeneralError('This is an error'); + } + return context.app.service('users').get(userId, context.params); } } diff --git a/packages/schema/test/hooks.test.ts b/packages/schema/test/hooks.test.ts index 0f0aa6a0d7..7b47fec4e9 100644 --- a/packages/schema/test/hooks.test.ts +++ b/packages/schema/test/hooks.test.ts @@ -24,7 +24,7 @@ describe('@feathersjs/schema/hooks', () => { }); }); - it('resolves results', async () => { + it('resolves results and handles resolver errors (#2534)', async () => { // eslint-disable-next-line const { password, ...externalUser } = user; const payload = { @@ -49,6 +49,24 @@ describe('@feathersjs/schema/hooks', () => { user: externalUser, ...payload }]); + + await assert.rejects(() => app.service('messages').find({ + provider: 'external', + error: true + }), { + name: 'BadRequest', + message: 'Error resolving data', + code: 400, + className: 'bad-request', + data: { + user: { + name: 'GeneralError', + message: 'This is an error', + code: 500, + className: 'general-error' + } + } + }); }); it('validates and converts the query', async () => {