Skip to content

Commit ca265d8

Browse files
authored
feat(ui): Change API request exceptions to use cause property (#37262)
Instead of parsing and mutating the error stack frames, use the [cause](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) property for API request errors.
1 parent 749d8ca commit ca265d8

File tree

3 files changed

+9
-29
lines changed

3 files changed

+9
-29
lines changed

static/app/api.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ export class Client {
551551
// This *should* get logged to Sentry only if the promise rejection is not handled
552552
// (since SDK captures unhandled rejections). Ideally we explicitly ignore rejection
553553
// or handle with a user friendly error message
554-
const preservedError = new Error();
554+
const preservedError = new Error('API Request Error');
555555

556556
return new Promise((resolve, reject) =>
557557
this.request(path, {
@@ -567,11 +567,10 @@ export class Client {
567567
error: (resp: ResponseMeta) => {
568568
const errorObjectToUse = createRequestError(
569569
resp,
570-
preservedError.stack,
570+
preservedError,
571571
options.method,
572572
path
573573
);
574-
errorObjectToUse.removeFrames(2);
575574

576575
// Although `this.request` logs all error responses, this error object can
577576
// potentially be logged by Sentry's unhandled rejection handler

static/app/utils/requestError/createRequestError.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ const ERROR_MAP = {
2525
*/
2626
export default function createRequestError(
2727
resp: ResponseMeta,
28-
stack: string | undefined,
28+
cause: Error,
2929
method: 'POST' | 'GET' | 'DELETE' | 'PUT' | undefined,
3030
path: string
3131
) {
32-
const err = new RequestError(method, path);
32+
const err = new RequestError(method, path, {cause});
3333

3434
if (resp) {
3535
const errorName = ERROR_MAP[resp.status];
@@ -41,9 +41,5 @@ export default function createRequestError(
4141
err.setResponse(resp);
4242
}
4343

44-
if (stack) {
45-
err.setStack(stack);
46-
}
47-
4844
return err;
4945
}

static/app/utils/requestError/requestError.tsx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import {ResponseMeta} from 'sentry/api';
22

33
import {sanitizePath} from './sanitizePath';
44

5+
interface ErrorOptionsObject {
6+
cause: Error;
7+
}
58
export default class RequestError extends Error {
69
responseText?: string;
710
responseJSON?: any;
811
status?: number;
912
statusText?: string;
1013

11-
constructor(method: string | undefined, path: string) {
12-
super(`${method || 'GET'} ${sanitizePath(path)}`);
14+
constructor(method: string | undefined, path: string, options: ErrorOptionsObject) {
15+
super(`${method || 'GET'} "${sanitizePath(path)}"`, options);
1316
this.name = 'RequestError';
1417
Object.setPrototypeOf(this, new.target.prototype);
1518
}
@@ -41,25 +44,7 @@ export default class RequestError extends Error {
4144
this.message = message;
4245
}
4346

44-
setStack(newStack: string) {
45-
this.stack = newStack;
46-
}
47-
4847
setName(name: string) {
4948
this.name = name;
5049
}
51-
52-
removeFrames(numLinesToRemove) {
53-
// Drop some frames so stack trace starts at callsite
54-
//
55-
// Note that babel will add a call to support extending Error object
56-
57-
// Old browsers may not have stack trace
58-
if (!this.stack) {
59-
return;
60-
}
61-
62-
const lines = this.stack.split('\n');
63-
this.stack = [lines[0], ...lines.slice(numLinesToRemove)].join('\n');
64-
}
6550
}

0 commit comments

Comments
 (0)