Skip to content

Commit 31fbdff

Browse files
committed
fix(schema): Properly handle resolver errors (#2540)
1 parent b9dfaee commit 31fbdff

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/schema/src/resolver.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { BadRequest } from '@feathersjs/errors';
2-
import { Schema } from './schema';
32

43
export type PropertyResolver<T, V, C> = (
54
value: V|undefined,
@@ -13,7 +12,9 @@ export type PropertyResolverMap<T, C> = {
1312
}
1413

1514
export interface ResolverConfig<T, C> {
16-
schema?: Schema<any>,
15+
// TODO this should be `Schema<any>` but has recently produced an error, see
16+
// https://github.com/ThomasAribart/json-schema-to-ts/issues/53
17+
schema?: any,
1718
validate?: 'before'|'after'|false,
1819
properties: PropertyResolverMap<T, C>
1920
}
@@ -94,7 +95,9 @@ export class Resolver<T, C> {
9495
}));
9596

9697
if (hasErrors) {
97-
throw new BadRequest(`Error resolving data ${status?.properties.join('.')}`, errors);
98+
const propertyName = status?.properties ? ` ${status.properties.join('.')}` : '';
99+
100+
throw new BadRequest('Error resolving data' + propertyName, errors);
98101
}
99102

100103
return schema && validate === 'after'

packages/schema/test/fixture.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
feathers, HookContext, Application as FeathersApplication
33
} from '@feathersjs/feathers';
44
import { memory, Service } from '@feathersjs/memory';
5+
import { GeneralError } from '@feathersjs/errors';
56

67
import {
78
schema, resolve, Infer, resolveResult,
@@ -86,6 +87,10 @@ export const messageResultResolver = resolve<MessageResult, HookContext<Applicat
8687
user: async (_value, message, context) => {
8788
const { userId } = message;
8889

90+
if (context.params.error === true) {
91+
throw new GeneralError('This is an error');
92+
}
93+
8994
return context.app.service('users').get(userId, context.params);
9095
}
9196
}

packages/schema/test/hooks.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('@feathersjs/schema/hooks', () => {
2424
});
2525
});
2626

27-
it('resolves results', async () => {
27+
it('resolves results and handles resolver errors (#2534)', async () => {
2828
// eslint-disable-next-line
2929
const { password, ...externalUser } = user;
3030
const payload = {
@@ -49,6 +49,24 @@ describe('@feathersjs/schema/hooks', () => {
4949
user: externalUser,
5050
...payload
5151
}]);
52+
53+
await assert.rejects(() => app.service('messages').find({
54+
provider: 'external',
55+
error: true
56+
}), {
57+
name: 'BadRequest',
58+
message: 'Error resolving data',
59+
code: 400,
60+
className: 'bad-request',
61+
data: {
62+
user: {
63+
name: 'GeneralError',
64+
message: 'This is an error',
65+
code: 500,
66+
className: 'general-error'
67+
}
68+
}
69+
});
5270
});
5371

5472
it('validates and converts the query', async () => {

0 commit comments

Comments
 (0)