Giter Club home page Giter Club logo

Comments (5)

firyalff avatar firyalff commented on August 12, 2024 1

First thing first, http errors are all about proper http code, so we should start designing error handler by http error code specification and errors thrown from controller, should include desired http error code (and error message) @adiferd

from expr.

firyalff avatar firyalff commented on August 12, 2024

for now, i use restify error event handler to handle exception in controller (i use restify for day to day work). Restify error handler wrapped in ErrorMap class. Node.js utilize next() method to move process to next chain, so the wrapper method called within node.js next method

return next(ErrorMap.mapping(error.status || 500, error.message || 'Internal server error'));

See this error map method snippet for reference

'use strict'
const errs = require('restify-errors');

class ErrorMap {
	static mapping (code, message = null) {
		const errorList = {
			400: 'BadRequestError',
			401: 'UnauthorizedError',
			403: 'ForbiddenError',
			404: 'NotFoundError',
			405: 'MethodNotAllowedError',
			408: 'RequestTimeoutError',
			413: 'RequestEntityTooLargeError',
			500: 'InternalServerError'
		};

		return new errs[errorList[code]](message);
	}	
}

module.exports= ErrorMap;

Since we are building this thing on top of express, we can't use restify's error handler event. Unfortunately, things going quite different in express for no error events are available, per this docs provided by express.

https://expressjs.com/en/guide/error-handling.html

So no other way than build error handler method by ourself @adiferd

from expr.

firyalff avatar firyalff commented on August 12, 2024

the next method called within exception in try catch block, which is the catch scope

from expr.

adiferd avatar adiferd commented on August 12, 2024

I think, I can start with refactor this function

'use strict'

class Formatter {
	static toSingleResponse(data, message, error) {
		return {
			  result: data
			, message: message
			, error: (error!=null)?error:null
		};
	}

remove the error key from the return and build the ErrorMapper Function

toSingleResponse will be used only to return success reponse, on the other hand errorMapper will be used to return error message on API request. what do you think @firyalff ?

from expr.

firyalff avatar firyalff commented on August 12, 2024

this is good, since we're gonna move error response formater to separated module, +1 for this @adiferd

from expr.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.