Koa provides a helper method for easily throwing errors with appropriate HTTP status codes. It uses http-errors for error creation. The ctx.throw() method throws an error with a .status property, which is 500 (Internal Server Error) by default. This error with the status property enables Koa to respond properly when different errors occur. The method has the signature, ctx.throw([status], [error], [properties]). The following different usages are permitted:
ctx.throw(401);
ctx.throw(401, 'Access denied to the resource');
ctx.throw(401, 'Access denied to the resource', { user });
Throwing ctx.throw(401, 'Access denied to the resource'), for example, is shorthand for the following:
const err = new Error('Access denied to the resource');
err.status = 401;
err.expose = true;
throw err;
It is important to note that these...