Skip to content

Commit fb2f052

Browse files
authored
feat(Error): add request details in error.message (#505)
1 parent 1bcf59d commit fb2f052

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/request.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,6 @@ const createApiUrl = ({
101101
return apiURL;
102102
};
103103

104-
// handle AV._request Error
105-
const handleError = (error) =>
106-
new Promise((resolve, reject) => {
107-
let errorJSON = {
108-
code: error.code || -1,
109-
error: error.message || error.responseText,
110-
};
111-
if (error.response && error.response.code) {
112-
errorJSON = error.response;
113-
} else if (error.responseText) {
114-
try {
115-
errorJSON = JSON.parse(error.responseText);
116-
} catch (e) {
117-
// If we fail to parse the error text, that's okay.
118-
}
119-
}
120-
121-
// Transform the error into an instance of AVError by trying to parse
122-
// the error string as JSON.
123-
reject(new AVError(errorJSON.code, errorJSON.error));
124-
});
125-
126104
/**
127105
* Low level REST API client. Call REST endpoints with authorization headers.
128106
* @function AV.request
@@ -144,7 +122,25 @@ const request = ({ service, version, method, path, query, data = {}, authOptions
144122
const url = createApiUrl({ service, path, version });
145123
return setHeaders(authOptions, signKey).then(
146124
headers => ajax({ method, url, query, data, headers })
147-
.catch(handleError)
125+
.catch((error) => {
126+
let errorJSON = {
127+
code: error.code || -1,
128+
error: error.message || error.responseText,
129+
};
130+
if (error.response && error.response.code) {
131+
errorJSON = error.response;
132+
} else if (error.responseText) {
133+
try {
134+
errorJSON = JSON.parse(error.responseText);
135+
} catch (e) {
136+
// If we fail to parse the error text, that's okay.
137+
}
138+
}
139+
errorJSON.error += ` [${method} ${url}]`;
140+
// Transform the error into an instance of AVError by trying to parse
141+
// the error string as JSON.
142+
throw new AVError(errorJSON.code, errorJSON.error);
143+
})
148144
);
149145
};
150146

test/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("User", function() {
4949
it("should fail with wrong password", function() {
5050
return AV.User.logIn(username, 'wrong password')
5151
.should.be.rejectedWith({
52-
message: 'The username and password mismatch.',
52+
message: /The username and password mismatch./,
5353
code: 210,
5454
});
5555
});

0 commit comments

Comments
 (0)