-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1656: Improve errors r=curquiza a=flevi29 # Pull Request ## Related issues Fixes #1612, #1655 ## What does this PR do? This PR aims to improve errors, so that they can contain all the necessary information, and more. - Prevent browser test `jsdom` from replacing `fetch` and `AbortController` for consistency with node tests, replacing previous solution where we removed the builtin `fetch` from node tests - Remove `"abort-controller"` package, it was only used in tests and now `AbortController` is always available - Rename `MeiliSearchCommunicationError` to `MeiliSearchRequestError`, as this error might not be entirely related to communication, but rather the request itself - Make errors use [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause), preserving the original error, simplifying things, taking advantage of modern browsers and runtimes actually printing this property - Remove the use of `Object.setPrototypeOf` in errors, this is not needed in modern browsers, and bundlers take care of it if we need to support older browsers (so in UMD bundle it's done twice currently). (https://stackoverflow.com/a/76851585) - Remove the use of `Error.captureStackTrace`, this is done by the base `Error` constructor, and it's only available in V8 engine based browsers/runtimes - https://v8.dev/docs/stack-trace-api - https://nodejs.org/api/errors.html#new-errormessage-options - https://stackoverflow.com/a/64063868 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack - Only catch the error from `fetch` to re-throw it as `MeiliSearchRequestError`, other potential errors should propagate as they're either truly unexpected or are thrown by us, simplifying error handling and not putting unexpected errors under the `MeiliSearchError` umbrella - Rename `MeiliSearchErrorInfo` type to `MeiliSearchErrorResponse` - Other minor changes/improvements NOTE: Tests are horrifying, I didn't change all that much in src, but I had to change almost every test and by quite a bit. Testing is what I should aim to improve ASAP. Co-authored-by: F. Levi <[email protected]> Co-authored-by: Bruno Casali <[email protected]>
- Loading branch information
Showing
45 changed files
with
437 additions
and
793 deletions.
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 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,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.