AyushKumar123456789 / stock-analysis-backend

Backend of stock analysis of project
2 stars 2 forks source link

feat: new better way of error handling #8

Closed michalgolda closed 1 month ago

michalgolda commented 1 month ago

Hi, this is my proposal of error handling in one center place (error handler function). This prevents code repeating issues (DRY) and increase readability and extensibility (Open/Closed principle from SOLID).

Code of async controller should be wrapped by try..catch (err) next(err) because express4 does not support error handling of promises.

Example response:

{
    "error": {
        "name": "VALIDATION_ERROR",
        "message": "You requested data is not valid.",
        "details": [
            {
                "path": "password",
                "message": "password is a required field"
            }
        ]
    }
}
{
    "error": {
        "name": "INTERNAL_SERVER_ERROR",
        "message": "Internal Server Error"
    }
}