Skip to content

Commit

Permalink
Fixed passing along readable error messages when a request returns wi…
Browse files Browse the repository at this point in the history
…th an error code
  • Loading branch information
anoek committed Dec 4, 2023
1 parent 5a813b8 commit 0679486
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/lib/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ export function errorAlerter(...args) {
});
}
console.error(err);
console.error(new Error().stack);
}
export function errorLogger(...args) {
const err = getPrintableError(args[0]);
Expand Down
27 changes: 20 additions & 7 deletions src/lib/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,34 @@ function request(method: Method): RequestFunction {
})
.then((res) => {
delete requests_in_flight[request_id];
if (res.status >= 200 && res.status < 300) {
if (res.status === 204) {
resolve({});

const onJson = (data: any) => {
if (res.status >= 200 && res.status < 300) {
if (res.status === 204) {
resolve({});
} else {
resolve(data);
}
} else {
resolve(res.json());
console.error(res.status, url, data);
console.error(traceback.stack);
reject(data);
}
};

const data_or_promise = res.json();

if (data_or_promise instanceof Promise) {
data_or_promise.then(onJson).catch(reject);
} else {
reject(res);
onJson(data_or_promise);
}
})
.catch((err) => {
delete requests_in_flight[request_id];
if (err.name !== "AbortError") {
console.warn(url, err.name);
console.warn(traceback.stack);
console.error(err.name, url);
console.error(traceback.stack);
}
reject(err);
});
Expand Down

0 comments on commit 0679486

Please sign in to comment.