Posted On January 13, 2022

How to change error status response in loopback node Js?

Ghazal Ehsan 0 comments
Hut Explore >> Programming , Technology >> How to change error status response in loopback node Js?
687474703a2f2f6c6f6f706261636b2e696f2f696d616765732f6272616e64696e672f6d61726b2f626c75652f6c6f6f706261636b2e6a7067

In LoopBack, default error status response is :

{
   "error":{
      "statusCode":401,
      "name":"Error",
      "message":"Authorization Required",
      "code":"AUTHORIZATION_REQUIRED",
      "stack":"Error: Authorization Required\n    at /var/www/project/loopback-mysql/node_modules/loopback/lib/application.js:430:21\n    at /var/www/project/loopback-mysql/node_modules/loopback/lib/model.js:358:7\n    at /var/www/project/loopback-mysql/node_modules/loopback/common/models/acl.js:529:16\n    at /var/www/project/loopback-mysql/node_modules/async/dist/async.js:3888:9\n    at /var/www/project/loopback-mysql/node_modules/async/dist/async.js:473:16\n    at iteratorCallback (/var/www/project/loopback-mysql/node_modules/async/dist/async.js:1064:13)\n    at /var/www/project/loopback-mysql/node_modules/async/dist/async.js:969:16\n    at /var/www/project/loopback-mysql/node_modules/async/dist/async.js:3885:13\n    at /var/www/project/loopback-mysql/node_modules/loopback/common/models/acl.js:511:17\n    at /var/www/project/loopback-mysql/node_modules/loopback/common/models/role.js:434:21\n    at process._tickCallback (internal/process/next_tick.js:61:11)"
   }
}

Step-by-step guide:

Following steps involved to change a default error response in loopback:

  1. Create error.js (or any name) in server/boot, if it is not already created.
  2. Open error.js file and write the following code.
module.exports = function(app) {
   var remotes = app.remotes();
   remotes.options.rest = remotes.options.rest || {};
   remotes.options.rest.handleErrors = false;
   app.middleware('final', FinalErrorHandler);
   function FinalErrorHandler(err, req, res, next) {
      if (err) {
          if (err.statusCode != 200) {
            res.status(err.statusCode).send({
               statusCode: err.statusCode,
               data: {},
               error: {
                 statusCode: err.statusCode,
                 name: 'Error',
                 message: err.message,
              },
            }).end();
         }
      }
      next();
   }
};

Now run node . command in terminal and check the response is change like this:

{
   "statusCode":401,
   "data":{},
   "error":{
      "statusCode":401,
      "name":"Error",
      "message":"Authorization Required"
   }
}

https://medium.com/@ghazaltaimur27/how-to-change-error-status-response-in-loopback-node-js-6db51f595006

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Binance.US CEO tells employees the company is growing faster than ever

Binance.US, the yankee company related to the world’s largest crypto exchange, isn’t troubled regarding the…

Create your own BelongsTo Required relation Mixin in loopback NodeJs

A mixin is nothing more than a method of two parameters:  function(Model, options): “Model” is the…

Create your own custom mixin in LoopBack NodeJs

A mixin is nothing more than a method of two parameters: function(Model, options): “Model” is the…