generated from MattHalloran/ReactGraphQLTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.ts
25 lines (23 loc) · 875 Bytes
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { ApolloError } from 'apollo-server-express';
import { logger, LogLevel } from './logger';
export class CustomError extends ApolloError {
constructor(error: any, message?: any, logMeta?: { [key: string]: any }) {
// Format error
super(message || error.message, error.code);
Object.defineProperty(this, 'name', { value: error.code });
// Log error, if logMeta is provided
if (logMeta) logger.log(LogLevel.error, message ?? error.message, logMeta);
}
}
export async function validateArgs(schema: any, args: any) {
try {
await schema.validate(args, { abortEarly: false });
} catch (err: any) {
logger.log(LogLevel.info, 'Failed to validate args', args);
throw new CustomError({
code: 'ARGS_VALIDATION_FAILED',
message: err.errors
})
}
return null;
}