Home > database >  Node-React always returns 500 instead of custom errors
Node-React always returns 500 instead of custom errors

Time:11-12

I'm having troubles returning errors on my server. when running on localhost - everything works fine, custom errors are returning great to the client. After deploying to a host (Heroku, Render etc...) the valid requests are working as expected, but when error is occuring - Im receiving 500 instead of the custom error I'd like to return.

Tried several error handlers and no work.

ERROR HANDLER (SERVER)

const errorHandler = (error, request, response, next) => {
    console.log(error);
    if (error.errorType !== undefined && error.errorType.isShowStackTrace){
        response.status(error.errorType.httpCode).json({message: error.errorType.message});
        return;
    }
    
    response.status(700).json({message: 'GENERAL ERROR OCCURED'});
}

module.exports = errorHandler;

REQUEST (CLIENT)

        try{
            const response = await axios.post("https://myUrl.com/users/login", user);
            localStorage.setItem("token", response.data.token);
            return response.data;
        }
        catch (err : any){
            return err;
        }

Expected custom error, and got 500

CodePudding user response:

All valid HTTP status codes are within the range of 100 to 599, inclusive.

The rfc9110 spec makes it clear that these are extensible, but only provided that you follow the rule above (emphasis added):

HTTP status codes are extensible. A client is not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. However, a client MUST understand the class of any status code, as indicated by the first digit, and treat an unrecognized status code as being equivalent to the x00 status code of that class.

In other words, systems need to know that you're sending a Server error, indicated by the fact that it starts with a 5 or a Client error that would start with a 4.

  • Related