-
Notifications
You must be signed in to change notification settings - Fork 30
fix: improve api error messages #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f4613f4
fix: improve api error messages
fmenezes 6bff85c
fix: send error as error
fmenezes 39599e2
Update src/common/atlas/apiClientError.ts
fmenezes 0799bee
fix: copilot error
fmenezes 1822068
fix: error message
fmenezes 03dea63
fix: type errors
fmenezes 5077fc0
fix: type errors
fmenezes b85e884
Merge branch 'main' into fmenezes/error_messages
fmenezes 7ea9854
fix: ApiClientError constructor
fmenezes 84d70ef
fix: add Error type
fmenezes 4a0db37
fix: compilation errors
fmenezes 3d4bee4
Merge branch 'main' into fmenezes/error_messages
fmenezes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 +1,67 @@ | ||
export class ApiClientError extends Error { | ||
response?: Response; | ||
import { ApiError } from "./openapi.js"; | ||
|
||
constructor(message: string, response: Response | undefined = undefined) { | ||
export class ApiClientError extends Error { | ||
private constructor( | ||
message: string, | ||
public readonly apiError?: ApiError | ||
) { | ||
super(message); | ||
this.name = "ApiClientError"; | ||
this.response = response; | ||
} | ||
|
||
static async fromResponse( | ||
response: Response, | ||
message: string = `error calling Atlas API` | ||
): Promise<ApiClientError> { | ||
const err = await this.extractError(response); | ||
|
||
return this.fromError(response, err, message); | ||
} | ||
|
||
static fromError( | ||
response: Response, | ||
error?: ApiError | string | Error, | ||
message: string = `error calling Atlas API` | ||
): ApiClientError { | ||
const errorMessage = this.buildErrorMessage(error); | ||
|
||
const apiError = typeof error === "object" && !(error instanceof Error) ? error : undefined; | ||
|
||
return new ApiClientError(`[${response.status} ${response.statusText}] ${message}: ${errorMessage}`, apiError); | ||
} | ||
|
||
private static async extractError(response: Response): Promise<ApiError | string | undefined> { | ||
try { | ||
const text = await response.text(); | ||
return new ApiClientError(`${message}: [${response.status} ${response.statusText}] ${text}`, response); | ||
return (await response.json()) as ApiError; | ||
} catch { | ||
return new ApiClientError(`${message}: ${response.status} ${response.statusText}`, response); | ||
try { | ||
return await response.text(); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
} | ||
|
||
private static buildErrorMessage(error?: string | ApiError | Error): string { | ||
let errorMessage: string = "unknown error"; | ||
|
||
if (error instanceof Error) { | ||
return error.message; | ||
} | ||
|
||
//eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check | ||
switch (typeof error) { | ||
case "object": | ||
errorMessage = error.reason || "unknown error"; | ||
if (error.detail && error.detail.length > 0) { | ||
errorMessage = `${errorMessage}; ${error.detail}`; | ||
} | ||
break; | ||
case "string": | ||
errorMessage = error; | ||
break; | ||
} | ||
|
||
return errorMessage.trim(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.