Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/schema/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BadRequest } from '@feathersjs/errors';
import { Schema } from './schema';

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

export interface ResolverConfig<T, C> {
schema?: Schema<any>,
// TODO this should be `Schema<any>` 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<T, C>
}
Expand Down Expand Up @@ -94,7 +95,9 @@ export class Resolver<T, C> {
}));

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'
Expand Down
5 changes: 5 additions & 0 deletions packages/schema/test/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -86,6 +87,10 @@ export const messageResultResolver = resolve<MessageResult, HookContext<Applicat
user: async (_value, message, context) => {
const { userId } = message;

if (context.params.error === true) {
throw new GeneralError('This is an error');
}

return context.app.service('users').get(userId, context.params);
}
}
Expand Down
20 changes: 19 additions & 1 deletion packages/schema/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 () => {
Expand Down