-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Infisical/daniel/less-verbose-errors
feat: less verbose errors
- Loading branch information
Showing
8 changed files
with
292 additions
and
125 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
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
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { AxiosError } from "axios"; | ||
|
||
type TApiErrorResponse = { | ||
statusCode: number; | ||
message: string; | ||
error: string; | ||
}; | ||
export class InfisicalSDKError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.message = message; | ||
this.name = "InfisicalSDKError"; | ||
} | ||
} | ||
|
||
export class InfisicalSDKRequestError extends Error { | ||
constructor( | ||
message: string, | ||
requestData: { | ||
url: string; | ||
method: string; | ||
statusCode: number; | ||
} | ||
) { | ||
super(message); | ||
this.message = `[URL=${requestData.url}] [Method=${requestData.method}] [StatusCode=${requestData.statusCode}] ${message}`; | ||
this.name = "InfisicalSDKRequestError"; | ||
} | ||
} | ||
|
||
export const newInfisicalError = (error: any) => { | ||
if (error instanceof AxiosError) { | ||
const data = error?.response?.data as TApiErrorResponse; | ||
|
||
if (data?.message) { | ||
return new InfisicalSDKRequestError(data.message, { | ||
url: error.response?.config.url || "", | ||
method: error.response?.config.method || "", | ||
statusCode: error.response?.status || 0 | ||
}); | ||
} else if (error.message) { | ||
return new InfisicalSDKError(error.message); | ||
} else if (error.code) { | ||
// If theres no message but a code is present, it's likely to be an aggregation error. This is not specific to Axios, but it falls under the AxiosError type | ||
return new InfisicalSDKError(error.code); | ||
} else { | ||
return new InfisicalSDKError("Request failed with unknown error"); | ||
} | ||
} | ||
|
||
return new InfisicalSDKError(error?.message || "An error occurred"); | ||
}; |
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
Oops, something went wrong.