How to use the got/errors.GotError function in got

To help you get started, we’ve selected a few got examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github roblav96 / robinhood.tools / src / server / api / fastify.errors.ts View on Github external
async function onErrorHandler(error: FastifyError, request: FastifyRequest, reply: FastifyReply, isCatcher = false) {
	if (error == null) error = boom.internal('onErrorHandler -> error == null');

	if (Array.isArray(error.validation)) {
		let validation = error.validation[0]
		let param = validation.dataPath.substr(1)
		param = param ? `'${param}'` : 'is missing,'
		let message = 'Parameter ' + param + ' ' + validation.message
		error = boom.preconditionFailed(message, error.validation)
	}

	if (error instanceof gerrors.GotError) {
		let gerror = (error as any) as Http.GotError
		error = new boom(gerror.message, {
			statusCode: gerror.statusCode,
			data: gerror.response.body,
		})
		error.isGot = true
	}

	if (!error.isBoom) error = new boom(error);

	if (!error.isCaught) {
		if (error.data) _.defaults(error.output.payload, { attributes: error.data });
		reply.type('application/json')
		reply.code(error.output.statusCode)
		reply.headers(error.output.headers)
		error.isCaught = true