本文整理汇总了TypeScript中Boom.isBoom函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isBoom函数的具体用法?TypeScript isBoom怎么用?TypeScript isBoom使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isBoom函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should create a Boom.badRequest error object', () => {
const error = Schema.createValidationError();
const errorWithData = Schema.createValidationError({
schema: exampleSchema,
data: {
id: 1,
name: 'John Smith',
comment: 'A long comment about this generic person',
},
});
// Assertions
expect(Boom.isBoom(error)).toBe(true);
expect(error.message).toBe('No data provided');
expect(error.data).toEqual({
errors: [],
schema: undefined,
});
expect(Boom.isBoom(errorWithData)).toBe(true);
expect(errorWithData.message).toBe('Invalid data provided');
expect(errorWithData.data).toEqual({
errors: [],
schema: exampleSchema,
validatedData: {
comment: 'A long comment about this generic person',
id: 1,
name: 'John Smith',
},
});
});
示例2: it
it('throws if either value is not an integer', () => {
let error;
try {
decodeVersion('WzEsMy41XQ==');
} catch (err) {
error = err;
}
expect(error.message).toMatchInlineSnapshot(`"Invalid version [WzEsMy41XQ==]"`);
expect(Boom.isBoom(error)).toBe(true);
expect(error.output).toMatchInlineSnapshot(`
Object {
"headers": Object {},
"payload": Object {
"error": "Bad Request",
"message": "Invalid version [WzEsMy41XQ==]",
"statusCode": 400,
},
"statusCode": 400,
}
`);
});
示例3: errorHandlerMiddleware
return async function errorHandlerMiddleware(ctx, next) {
try {
await next();
if (ctx.status === 404) throw Boom.notFound();
} catch (applicationError) {
const err: ApplicationError = applicationError;
let error: Boom<any>;
// Normalize error object
try {
if (!(applicationError instanceof Error)) throw new Error('Cannot handle non-errors as errors');
error = Boom.isBoom(err)
? err
: Boom.boomify(err, {
statusCode: err.status || err.statusCode || undefined,
decorate: err.data,
});
} catch (subError) {
error = Boom.boomify(subError);
}
// Set defaults
let status = Boom.notAcceptable().output.statusCode;
let headers = {};
let body: string | object = Boom.notAcceptable().output.payload.message;
// Check for dev and include dev stuff
if (ctx.app.env !== 'production') {
error.output.payload.additionalDevelopmentData = {
data: error.data || undefined,
stack: error.isServer && error.stack ? error.stack : undefined,
};
}
// Convert boom response to proper format
const payload = {
error: {
code: error.output.payload.code || '',
status: error.output.payload.statusCode,
error: error.output.payload.error,
message: error.output.payload.message,
errors: error.output.payload.errors,
additionalDevelopmentData: error.output.payload.additionalDevelopmentData,
},
};
// Respond with the proper format
const format = ctx.accepts(['json', 'text']);
if (format === 'json') {
status = error.output.statusCode;
headers = error.output.headers;
body = payload;
} else if (format === 'text') {
status = error.output.statusCode;
headers = error.output.headers;
body = payload.error.message;
}
// Respond with the error
ctx.set(headers);
ctx.status = status;
ctx.body = body;
}
};
示例4: getErrorStatusCode
export function getErrorStatusCode(error: any): number {
return Boom.isBoom(error) ? error.output.statusCode : error.statusCode || error.status;
}