Skip to content

Instantly share code, notes, and snippets.

@leomao10
Last active July 18, 2019 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leomao10/f3af22ee51ee4f6052af6ac527508f9e to your computer and use it in GitHub Desktop.
Save leomao10/f3af22ee51ee4f6052af6ac527508f9e to your computer and use it in GitHub Desktop.
Proposed error handling
const { generic_error_handler } = require('./lib/generice_error_handler');
const { BudgetsService } = require('./lib/generice_error_handler');
module.exports = {
uploadBudget: async (request, h) => {
try {
const response = await BudgetsService.get({ budgetId });
}
cacth(e) {
if(e instanceof BudgetServiceForbidden) { throw Boom.forbidden(e.get.FORBIDDEN); }
if(e instanceof ApiBaseError) { generic_error_handler(e); }
// reraise error, so that it can be handle generically in plugin
throw e;
}
}
}
const { ApiUnauthorized, ApiForbidden, ApiBaseError } = require('./lib/errors');
// This can be abtracted in to a generic plugins so that we don't have to call it in every method
module.exports = {
generic_error_handler: (error) => {
if(error instanceof ApiUnauthorized){ throw Boom.unauthorized(); }
if(error instanceof ApiForbidden){ throw Boom.forbidden(); }
if(error instanceof ApiNotFound){ throw Boom.notFound(); }
throw Boom.badImplementation(err);
}
}
const { ApiUnauthorized, ApiForbidden, ApiBaseError } = require('./lib/errors');
class ReportsService {
static async getOrganizationDetails({ orgId, container }) {
const response = await fetch();
switch (response.status) {
case 401: throw new ApiUnauthorized(response);
case 403: throw new ApiForbidden(response);
default: throw new ApiBaseError(response);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment