mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding HTTP status code to error.toJSON (#2956)
* Adding HTTP status code to error.toJSON (axios#2947) * Adding Error display div to internal server client.html Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -35,7 +35,8 @@ module.exports = function enhanceError(error, config, code, request, response) {
|
|||||||
stack: this.stack,
|
stack: this.stack,
|
||||||
// Axios
|
// Axios
|
||||||
config: this.config,
|
config: this.config,
|
||||||
code: this.code
|
code: this.code,
|
||||||
|
status: this.response && this.response.status ? this.response.status : null
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
return error;
|
return error;
|
||||||
|
|||||||
+9
-1
@@ -5,7 +5,6 @@
|
|||||||
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
|
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
pre {
|
pre {
|
||||||
max-height: 200px;
|
|
||||||
min-height: 39px;
|
min-height: 39px;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
}
|
}
|
||||||
@@ -58,6 +57,11 @@
|
|||||||
<pre id="response">No Data</pre>
|
<pre id="response">No Data</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="well">
|
||||||
|
<h3>Error</h3>
|
||||||
|
<pre id="error">None</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script src="/axios.js"></script>
|
<script src="/axios.js"></script>
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
(function () {
|
||||||
@@ -81,6 +85,7 @@
|
|||||||
var submit = document.getElementById('submit');
|
var submit = document.getElementById('submit');
|
||||||
var request = document.getElementById('request');
|
var request = document.getElementById('request');
|
||||||
var response = document.getElementById('response');
|
var response = document.getElementById('response');
|
||||||
|
var error = document.getElementById('error');
|
||||||
|
|
||||||
function acceptsData(method) {
|
function acceptsData(method) {
|
||||||
return ['PATCH', 'POST', 'PUT'].indexOf(method) > -1;
|
return ['PATCH', 'POST', 'PUT'].indexOf(method) > -1;
|
||||||
@@ -138,8 +143,11 @@
|
|||||||
axios(options)
|
axios(options)
|
||||||
.then(function (res) {
|
.then(function (res) {
|
||||||
response.innerHTML = JSON.stringify(res.data, null, 2);
|
response.innerHTML = JSON.stringify(res.data, null, 2);
|
||||||
|
error.innerHTML = "None";
|
||||||
})
|
})
|
||||||
.catch(function (res) {
|
.catch(function (res) {
|
||||||
|
error.innerHTML = JSON.stringify(res.toJSON(), null, 2)
|
||||||
|
console.error('Axios caught an error from request', res.toJSON());
|
||||||
response.innerHTML = JSON.stringify(res.data, null, 2);
|
response.innerHTML = JSON.stringify(res.data, null, 2);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ describe('core::createError', function() {
|
|||||||
expect(json.message).toBe('Boom!');
|
expect(json.message).toBe('Boom!');
|
||||||
expect(json.config).toEqual({ foo: 'bar' });
|
expect(json.config).toEqual({ foo: 'bar' });
|
||||||
expect(json.code).toBe('ESOMETHING');
|
expect(json.code).toBe('ESOMETHING');
|
||||||
|
expect(json.status).toBe(200);
|
||||||
expect(json.request).toBe(undefined);
|
expect(json.request).toBe(undefined);
|
||||||
expect(json.response).toBe(undefined);
|
expect(json.response).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
var enhanceError = require('../../../lib/core/enhanceError');
|
var enhanceError = require('../../../lib/core/enhanceError');
|
||||||
|
|
||||||
describe('core::enhanceError', function() {
|
describe('core::enhanceError', function() {
|
||||||
it('should add config, config, request and response to error', function() {
|
it('should add config, code, request, response, and toJSON function to error', function() {
|
||||||
var error = new Error('Boom!');
|
var error = new Error('Boom!');
|
||||||
var request = { path: '/foo' };
|
var request = { path: '/foo' };
|
||||||
var response = { status: 200, data: { foo: 'bar' } };
|
var response = { status: 200, data: { foo: 'bar' } };
|
||||||
@@ -11,9 +11,19 @@ describe('core::enhanceError', function() {
|
|||||||
expect(error.code).toBe('ESOMETHING');
|
expect(error.code).toBe('ESOMETHING');
|
||||||
expect(error.request).toBe(request);
|
expect(error.request).toBe(request);
|
||||||
expect(error.response).toBe(response);
|
expect(error.response).toBe(response);
|
||||||
|
expect(typeof error.toJSON).toBe('function');
|
||||||
expect(error.isAxiosError).toBe(true);
|
expect(error.isAxiosError).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should serialize to JSON with a status of null when there is no response', function() {
|
||||||
|
var error = new Error('Boom!');
|
||||||
|
var request = { path: '/foo' };
|
||||||
|
var response = undefined;
|
||||||
|
|
||||||
|
var errorAsJson = enhanceError(error, { foo: 'bar' }, 'ESOMETHING', request, response).toJSON();
|
||||||
|
expect(errorAsJson.status).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
it('should return error', function() {
|
it('should return error', function() {
|
||||||
var error = new Error('Boom!');
|
var error = new Error('Boom!');
|
||||||
expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error);
|
expect(enhanceError(error, { foo: 'bar' }, 'ESOMETHING')).toBe(error);
|
||||||
|
|||||||
Reference in New Issue
Block a user