How do I get HTTP status code in case of 401?
Here is my code:
var result = HTTP.call("POST", 'http://example.com', { data:{"key": "value"}, headers:{'Content-Type' : 'application/json'}});
console.dir(result );
In case of error i get this exception:
I20200326-22:23:49.080(5)? 'Error: failed [401] {"data":null,"description":"Invalid Password!","status":"ERROR","statusCode":4012}\n' +
I20200326-22:23:49.080(5)? ' at Object.exports.makeErrorByStatus (packages/http.js:176:10)\n' +
I20200326-22:23:49.080(5)? ' at Request._callback (packages/http.js:140:24)\n' +
I20200326-22:23:49.080(5)? ' at Request.self.callback (/home/my-server/.meteor/packages/http/.1.4.2.8kf6jd.1nsqm++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/request/request.js:185:22)\n' +
I20200326-22:23:49.080(5)? ' at Request.emit (events.js:311:20)\n' +
I20200326-22:23:49.081(5)? ' at Request.EventEmitter.emit (domain.js:482:12)\n' +
I20200326-22:23:49.081(5)? ' at Request.<anonymous> (/home/my-server/.meteor/packages/http/.1.4.2.8kf6jd.1nsqm++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/request/request.js:1161:10)\n' +
I20200326-22:23:49.081(5)? ' at Request.emit (events.js:311:20)\n' +
I20200326-22:23:49.082(5)? ' at Request.EventEmitter.emit (domain.js:482:12)\n' +
I20200326-22:23:49.082(5)? ' at IncomingMessage.<anonymous> (/home/my-server/.meteor/packages/http/.1.4.2.8kf6jd.1nsqm++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/request/request.js:1083:12)\n' +
I20200326-22:23:49.082(5)? ' at Object.onceWrapper (events.js:417:28)\n' +
I20200326-22:23:49.082(5)? ' at IncomingMessage.emit (events.js:323:22)\n' +
I20200326-22:23:49.082(5)? ' at IncomingMessage.EventEmitter.emit (domain.js:482:12)\n' +
I20200326-22:23:49.082(5)? ' at endReadableNT (_stream_readable.js:1204:12)\n' +
I20200326-22:23:49.083(5)? ' at processTicksAndRejections (internal/process/task_queues.js:84:21)'
How can I get error status code from this?
Wrap it to try{}catch(){}
block or use asynchronous version with callback.
try {
var result = HTTP.call("POST", 'http://example.com', { data:{"key": "value"}, headers:{'Content-Type' : 'application/json'}});
console.dir(result);
} catch (error) {
console.log(error);
}
@dr.dimitru This is what I get in catch too. Its the same for asynchronous too
What exactly are you trying to accomplish? What’s your goal?
In case of Error Code 401, I want to apply some logic on the response I received from API. I want to use that response data.
Try this:
const processStatusCode = (code) => {
if (code === 401) {
// your logic here
}
};
try {
const result = HTTP.call("POST", 'http://example.com', { data:{"key": "value"}, headers:{'Content-Type' : 'application/json'}});
processStatusCode(result.statusCode);
} catch (error) {
processStatusCode(error.response.statusCode);
}
1 Like
Its the same. I also want to take some decisions on the response data, not just the error code
Well, update and adapt the code I’ve posted to meet your needs.
But its not working bro. I am getting the same error.
Exception or error?
Exception or error?
Exception:
'Error: failed [401] {"data":null,"description":"Invalid Password!","status":"ERROR","statusCode":4012}\n' +
FYI, error.statusCode
is undefined
in catch
What if you console.log(error)
?
Ok here is the complete scenario:
Code:
try {
var result = HTTP.call("POST", srcUrl, { data:postFields, headers:headersData });
console.log('-- In Try --');
console.dir(result);
}
catch (error) {
console.log('-- In Catch --');
console.log(error);
console.log(error.statusCode);
}
Result:
I20200327-02:46:56.570(5)? -- In Catch --
I20200327-02:46:56.572(5)? Error: failed [401] {"data":null,"description":"Invalid Password!","status":"ERROR","statusCode":4012}
I20200327-02:46:56.573(5)? at Object.exports.makeErrorByStatus (packages/http.js:176:10)
I20200327-02:46:56.573(5)? at Request._callback (packages/http.js:140:24)
I20200327-02:46:56.573(5)? at Request.self.callback (/home/my-server/.meteor/packages/http/.1.4.2.8kf6jd.1nsqm++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/request/request.js:185:22)
I20200327-02:46:56.573(5)? at Request.emit (events.js:311:20)
I20200327-02:46:56.573(5)? at Request.EventEmitter.emit (domain.js:482:12)
I20200327-02:46:56.573(5)? at Request.<anonymous> (/home/my-server/.meteor/packages/http/.1.4.2.8kf6jd.1nsqm++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/request/request.js:1161:10)
I20200327-02:46:56.574(5)? at Request.emit (events.js:311:20)
I20200327-02:46:56.574(5)? at Request.EventEmitter.emit (domain.js:482:12)
I20200327-02:46:56.574(5)? at IncomingMessage.<anonymous> (/home/my-server/.meteor/packages/http/.1.4.2.8kf6jd.1nsqm++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/request/request.js:1083:12)
I20200327-02:46:56.574(5)? at Object.onceWrapper (events.js:417:28)
I20200327-02:46:56.574(5)? at IncomingMessage.emit (events.js:323:22)
I20200327-02:46:56.574(5)? at IncomingMessage.EventEmitter.emit (domain.js:482:12)
I20200327-02:46:56.574(5)? at endReadableNT (_stream_readable.js:1204:12)
I20200327-02:46:56.574(5)? at processTicksAndRejections (internal/process/task_queues.js:84:21) {
I20200327-02:46:56.575(5)? response: {
I20200327-02:46:56.575(5)? statusCode: 401,
I20200327-02:46:56.575(5)? content: '{"data":null,"description":"Invalid Password!","status":"ERROR","statusCode":4012}',
I20200327-02:46:56.575(5)? headers: {
I20200327-02:46:56.575(5)? vary: 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers, Origin, Access-Control-Request-Method, Access-Control-Request-Headers',
I20200327-02:46:56.575(5)? 'x-content-type-options': 'nosniff',
I20200327-02:46:56.575(5)? 'x-xss-protection': '1; mode=block',
I20200327-02:46:56.575(5)? 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
I20200327-02:46:56.576(5)? pragma: 'no-cache',
I20200327-02:46:56.576(5)? expires: '0',
I20200327-02:46:56.576(5)? 'x-frame-options': 'DENY',
I20200327-02:46:56.576(5)? 'content-type': 'application/json',
I20200327-02:46:56.576(5)? 'transfer-encoding': 'chunked',
I20200327-02:46:56.576(5)? date: 'Thu, 26 Mar 2020 21:46:56 GMT',
I20200327-02:46:56.576(5)? connection: 'close'
I20200327-02:46:56.576(5)? },
I20200327-02:46:56.576(5)? data: {
I20200327-02:46:56.577(5)? data: null,
I20200327-02:46:56.577(5)? description: 'Invalid Password!',
I20200327-02:46:56.577(5)? status: 'ERROR',
I20200327-02:46:56.577(5)? statusCode: 4012
I20200327-02:46:56.577(5)? }
I20200327-02:46:56.577(5)? }
I20200327-02:46:56.577(5)? }
I20200327-02:46:56.577(5)? undefined
So it isn’t throwing exception, it’s caught error.
Updated:
const processStatusCode = (code) => {
if (code === 401) {
// your logic here
}
};
try {
const result = HTTP.call("POST", 'http://example.com', { data:{"key": "value"}, headers:{'Content-Type' : 'application/json'}});
processStatusCode(result.statusCode);
} catch (error) {
processStatusCode(error.response.statusCode);
}
3 Likes
Nice. It worked like a charm. Thanks for the help bro,
1 Like