Skip to content

Commit 7472f00

Browse files
authored
fix: handle error raised by request failure (#409)
- copy error.code and error.message when error is raised by a request failure
1 parent 310cd08 commit 7472f00

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/request.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -204,37 +204,37 @@ const cacheServerURL = (serverURL, ttl) => {
204204
};
205205

206206
// handle AV._request Error
207-
const handleError = (res) => {
208-
const promise = new AVPromise();
209-
/**
210-
When API request need to redirect to the right location,
211-
can't use browser redirect by http status 307, as the reason of CORS,
212-
so API server response http status 410 and the param "location" for this case.
213-
*/
214-
if (res.statusCode === 410) {
215-
cacheServerURL(res.response.api_server, res.response.ttl).then(() => {
216-
promise.resolve(res.response.location);
217-
}).catch((error) => {
218-
promise.reject(error);
219-
});
220-
} else {
221-
let errorJSON = { code: -1, error: res.responseText };
222-
if (res.response && res.response.code) {
223-
errorJSON = res.response;
224-
} else if (res.responseText) {
225-
try {
226-
errorJSON = JSON.parse(res.responseText);
227-
} catch (e) {
228-
// If we fail to parse the error text, that's okay.
207+
const handleError = (error) => {
208+
return new AVPromise((resolve, reject) => {
209+
/**
210+
When API request need to redirect to the right location,
211+
can't use browser redirect by http status 307, as the reason of CORS,
212+
so API server response http status 410 and the param "location" for this case.
213+
*/
214+
if (error.statusCode === 410) {
215+
cacheServerURL(error.response.api_server, error.response.ttl).then(() => {
216+
resolve(error.response.location);
217+
}).catch(reject);
218+
} else {
219+
let errorJSON = {
220+
code: error.code || -1,
221+
error: error.message || error.responseText
222+
};
223+
if (error.response && error.response.code) {
224+
errorJSON = error.response;
225+
} else if (error.responseText) {
226+
try {
227+
errorJSON = JSON.parse(error.responseText);
228+
} catch (e) {
229+
// If we fail to parse the error text, that's okay.
230+
}
229231
}
230-
}
231232

232-
// Transform the error into an instance of AVError by trying to parse
233-
// the error string as JSON.
234-
const error = new AVError(errorJSON.code, errorJSON.error);
235-
promise.reject(error);
236-
}
237-
return promise;
233+
// Transform the error into an instance of AVError by trying to parse
234+
// the error string as JSON.
235+
reject(new AVError(errorJSON.code, errorJSON.error));
236+
}
237+
});
238238
};
239239

240240
const setServerUrl = (serverURL) => {

0 commit comments

Comments
 (0)