-
Notifications
You must be signed in to change notification settings - Fork 34
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 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,41 @@ | ||
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 response?: Response, | ||
public readonly body?: ApiError | ||
) { | ||
super(message); | ||
this.name = "ApiClientError"; | ||
this.response = response; | ||
} | ||
|
||
static async fromResponse( | ||
response: Response, | ||
message: string = `error calling Atlas API` | ||
): Promise<ApiClientError> { | ||
let text: string = ""; | ||
fmenezes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let body: ApiError | undefined = undefined; | ||
try { | ||
const text = await response.text(); | ||
return new ApiClientError(`${message}: [${response.status} ${response.statusText}] ${text}`, response); | ||
body = (await response.json()) as ApiError; | ||
text = body.reason || "unknown error"; | ||
if (body.detail && body.detail.length > 0) { | ||
text = `${text}; ${body.detail}`; | ||
} | ||
} catch { | ||
return new ApiClientError(`${message}: ${response.status} ${response.statusText}`, response); | ||
try { | ||
text = await response.text(); | ||
} catch { | ||
text = ""; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we set to something like "couldn't fetch error" or "unknown error" too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
|
||
if (text.length > 0) { | ||
text = `${message}: [${response.status} ${response.statusText}] ${text.trim()}`; | ||
} else { | ||
text = `${message}: ${response.status} ${response.statusText}`; | ||
} | ||
|
||
return new ApiClientError(text, response, body); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a benefit to us storing the body?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the ApiError model gives us insight into backend error codes, some tools might require an if/else depending on the backend error code.