Skip to content

Commit cd7ff04

Browse files
Adding HTTP status code to error.toJSON (#2956)
* Adding HTTP status code to error.toJSON (#2947) * Adding Error display div to internal server client.html Co-authored-by: Jay <jasonsaayman@gmail.com>
1 parent b5a1a67 commit cd7ff04

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

lib/core/enhanceError.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = function enhanceError(error, config, code, request, response) {
3535
stack: this.stack,
3636
// Axios
3737
config: this.config,
38-
code: this.code
38+
code: this.code,
39+
status: this.response && this.response.status ? this.response.status : null
3940
};
4041
};
4142
return error;

sandbox/client.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
66
<style type="text/css">
77
pre {
8-
max-height: 200px;
98
min-height: 39px;
109
overflow: auto;
1110
}
@@ -58,6 +57,11 @@ <h3>Response</h3>
5857
<pre id="response">No Data</pre>
5958
</div>
6059

60+
<div class="well">
61+
<h3>Error</h3>
62+
<pre id="error">None</pre>
63+
</div>
64+
6165
<script src="/axios.js"></script>
6266
<script>
6367
(function () {
@@ -81,6 +85,7 @@ <h3>Response</h3>
8185
var submit = document.getElementById('submit');
8286
var request = document.getElementById('request');
8387
var response = document.getElementById('response');
88+
var error = document.getElementById('error');
8489

8590
function acceptsData(method) {
8691
return ['PATCH', 'POST', 'PUT'].indexOf(method) > -1;
@@ -138,8 +143,11 @@ <h3>Response</h3>
138143
axios(options)
139144
.then(function (res) {
140145
response.innerHTML = JSON.stringify(res.data, null, 2);
146+
error.innerHTML = "None";
141147
})
142148
.catch(function (res) {
149+
error.innerHTML = JSON.stringify(res.toJSON(), null, 2)
150+
console.error('Axios caught an error from request', res.toJSON());
143151
response.innerHTML = JSON.stringify(res.data, null, 2);
144152
});
145153
};

test/specs/core/createError.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('core::createError', function() {
2323
expect(json.message).toBe('Boom!');
2424
expect(json.config).toEqual({ foo: 'bar' });
2525
expect(json.code).toBe('ESOMETHING');
26+
expect(json.status).toBe(200);
2627
expect(json.request).toBe(undefined);
2728
expect(json.response).toBe(undefined);
2829
});

test/specs/core/enhanceError.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var enhanceError = require('../../../lib/core/enhanceError');
22

33
describe('core::enhanceError', function() {
4-
it('should add config, config, request and response to error', function() {
4+
it('should add config, code, request, response, and toJSON function to error', function() {
55
var error = new Error('Boom!');
66
var request = { path: '/foo' };
77
var response = { status: 200, data: { foo: 'bar' } };
@@ -11,9 +11,19 @@ describe('core::enhanceError', function() {
1111
expect(error.code).toBe('ESOMETHING');
1212
expect(error.request).toBe(request);
1313
expect(error.response).toBe(response);
14+
expect(typeof error.toJSON).toBe('function');
1415
expect(error.isAxiosError).toBe(true);
1516
});
1617

18+
it('should serialize to JSON with a status of null when there is no response', function() {
19+
var error = new Error('Boom!');
20+
var request = { path: '/foo' };
21+
var response = undefined;
22+
23+
var errorAsJson = enhanceError(error, { foo: 'bar' }, 'ESOMETHING', request, response).toJSON();
24+
expect(errorAsJson.status).toEqual(null);
25+
});
26+
1727
it('should return error', function() {
1828
var error = new Error('Boom!');
1929
expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error);

0 commit comments

Comments
 (0)