-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update package config error, replaced with interface
- Loading branch information
1 parent
55761e7
commit 9d62023
Showing
1 changed file
with
3 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,4 @@ | ||
import { AxiosError } from "axios"; | ||
|
||
const ANSI_ESCAPE_CODE_RED_COLOR = "\x1b[31m%s\x1b[0m"; | ||
const DEFAULT_ERROR_MESSAGE = "Something went wrong"; | ||
|
||
export class AniwatchError extends Error { | ||
public scraper: string; | ||
|
||
constructor(errMsg: string, scraperName: string) { | ||
super(`${scraperName}: ${errMsg}`); | ||
|
||
this.name = AniwatchError.name; | ||
this.scraper = scraperName; | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, AniwatchError); | ||
} | ||
|
||
this.logError(); | ||
} | ||
|
||
static wrapError(err: AniwatchError | any, scraperName: string) { | ||
if (err instanceof AniwatchError) { | ||
return err; | ||
} | ||
|
||
if (err instanceof AxiosError) { | ||
const statusText = err?.response?.statusText || DEFAULT_ERROR_MESSAGE; | ||
return new AniwatchError("fetch_error: " + statusText, scraperName); | ||
} | ||
|
||
return new AniwatchError( | ||
err?.message || DEFAULT_ERROR_MESSAGE, | ||
scraperName | ||
); | ||
} | ||
|
||
private logError() { | ||
console.error( | ||
ANSI_ESCAPE_CODE_RED_COLOR, | ||
JSON.stringify( | ||
{ | ||
scraper: this.scraper, | ||
message: this?.message || DEFAULT_ERROR_MESSAGE, | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
} | ||
export interface AniwatchError extends Error { | ||
scraper: string; | ||
status: number; | ||
} |