Skip to content

Commit a2f66f1

Browse files
Improve error classes to avoid duplicate code
1 parent 08fa891 commit a2f66f1

File tree

1 file changed

+9
-13
lines changed
  • browser-extension/src/scripts

1 file changed

+9
-13
lines changed

browser-extension/src/scripts/http.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,20 @@ const fetchResponse = async (url: string, options?: RequestInit) => {
3333
return ret
3434
}
3535

36-
export class HttpServiceError extends Error {
37-
detail?: string
36+
function errorWithDetail<T extends new (...args: any[]) => any>(Base: T) {
37+
return class extends Base {
38+
detail?: string
3839

39-
constructor(detail?: string) {
40-
super()
41-
this.detail = detail
40+
constructor(...args: any[]) {
41+
super(args[0])
42+
this.detail = args[0]
43+
}
4244
}
43-
4445
}
4546

46-
export class NetworkError extends TypeError {
47-
detail?: string
47+
export class HttpServiceError extends errorWithDetail(Error) {}
4848

49-
constructor(detail?: string) {
50-
super()
51-
this.detail = detail
52-
}
53-
}
49+
export class NetworkError extends errorWithDetail(TypeError) {}
5450

5551
export async function* fetchStreamJson(url: string, options?: RequestInit): AsyncIterable<any> {
5652
let resp = await fetchResponse(url, options)

0 commit comments

Comments
 (0)