Last active
July 18, 2019 02:59
-
-
Save leomao10/f3af22ee51ee4f6052af6ac527508f9e to your computer and use it in GitHub Desktop.
Proposed error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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