-
Notifications
You must be signed in to change notification settings - Fork 85
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
Improve errors #1656
Open
flevi29
wants to merge
10
commits into
meilisearch:main
Choose a base branch
from
flevi29:improve-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improve errors #1656
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
77fa8e1
Improved errors and error handling in http-requests
flevi29 66a6c02
Adapted tests in client.test.ts, added more metadata to errors
flevi29 778bd06
Adapted most tests to changes
flevi29 6ed75e0
Adapted error tests, dropped one test case as it no longer applies
flevi29 aa5e8e1
Formatting and linting applied
flevi29 12dad0e
Fixed jsdom trying to polyfill AbortController
flevi29 96d5ba7
Fixed array checks in tests
flevi29 bc4ebb4
Format to try and ease merge to main
flevi29 4e55a32
Merge with format changes
flevi29 d3f490f
Delete accidentally re-included file from merge
flevi29 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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,6 +1,5 @@ | ||
export * from './http-error-handler'; | ||
export * from './meilisearch-api-error'; | ||
export * from './meilisearch-communication-error'; | ||
export * from './meilisearch-request-error'; | ||
export * from './meilisearch-error'; | ||
export * from './meilisearch-timeout-error'; | ||
export * from './version-hint-message'; |
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,30 +1,20 @@ | ||
import { MeiliSearchErrorInfo } from '../types'; | ||
import { MeiliSearchErrorResponse } from '../types'; | ||
import { MeiliSearchError } from './meilisearch-error'; | ||
|
||
const MeiliSearchApiError = class extends MeiliSearchError { | ||
httpStatus: number; | ||
code: string; | ||
link: string; | ||
type: string; | ||
stack?: string; | ||
export class MeiliSearchApiError extends MeiliSearchError { | ||
override name = 'MeiliSearchApiError'; | ||
override cause?: MeiliSearchErrorResponse; | ||
readonly response: Response; | ||
|
||
constructor(error: MeiliSearchErrorInfo, status: number) { | ||
super(error.message); | ||
constructor(response: Response, responseBody?: MeiliSearchErrorResponse) { | ||
super( | ||
responseBody?.message ?? `${response.status}: ${response.statusText}`, | ||
); | ||
|
||
// Make errors comparison possible. ex: error instanceof MeiliSearchApiError. | ||
Object.setPrototypeOf(this, MeiliSearchApiError.prototype); | ||
this.response = response; | ||
|
||
this.name = 'MeiliSearchApiError'; | ||
|
||
this.code = error.code; | ||
this.type = error.type; | ||
this.link = error.link; | ||
this.message = error.message; | ||
this.httpStatus = status; | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, MeiliSearchApiError); | ||
if (responseBody !== undefined) { | ||
this.cause = responseBody; | ||
} | ||
} | ||
}; | ||
export { MeiliSearchApiError }; | ||
} |
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,16 +1,7 @@ | ||
class MeiliSearchError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
export class MeiliSearchError extends Error { | ||
override name = 'MeiliSearchError'; | ||
|
||
// Make errors comparison possible. ex: error instanceof MeiliSearchError. | ||
Object.setPrototypeOf(this, MeiliSearchError.prototype); | ||
|
||
this.name = 'MeiliSearchError'; | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, MeiliSearchError); | ||
} | ||
constructor(...params: ConstructorParameters<typeof Error>) { | ||
super(...params); | ||
} | ||
} | ||
|
||
export { MeiliSearchError }; |
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,9 @@ | ||
import { MeiliSearchError } from './meilisearch-error'; | ||
|
||
export class MeiliSearchRequestError extends MeiliSearchError { | ||
override name = 'MeiliSearchRequestError'; | ||
|
||
constructor(url: string, cause: unknown) { | ||
super(`Request to ${url} has failed`, { cause }); | ||
} | ||
} |
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,18 +1,9 @@ | ||
import { MeiliSearchError } from './meilisearch-error'; | ||
|
||
class MeiliSearchTimeOutError extends MeiliSearchError { | ||
export class MeiliSearchTimeOutError extends MeiliSearchError { | ||
override name = 'MeiliSearchTimeOutError'; | ||
|
||
constructor(message: string) { | ||
super(message); | ||
|
||
// Make errors comparison possible. ex: error instanceof MeiliSearchTimeOutError. | ||
Object.setPrototypeOf(this, MeiliSearchTimeOutError.prototype); | ||
|
||
this.name = 'MeiliSearchTimeOutError'; | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, MeiliSearchTimeOutError); | ||
} | ||
} | ||
} | ||
|
||
export { MeiliSearchTimeOutError }; |
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
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.
Previously if
response.json()
failed, we swallowed the error. There are cases when the response body is empty, and is parsed to an empty string initially. That is an invalid JSON. This case was handled by simply swallowing the error, now it's handled explicitly, and if for whatever reason Meilisearch returns something non-JSON-valid other than an empty body, it will err, which is what we want, as that's entirely unexpected and should propagate.