Repository navigation
Recognise boxed Error subclasses as Errors #7
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
+151
−5
Merged
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions
40
test/runtime/instanceof-error-over-boxed-error-subclass.runtime.ts
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| class ParseFailure extends Error { | ||
| status: number | ||
| constructor(status: number, message: string, name = 'Error') { | ||
| super(message) | ||
| this.name = name | ||
| this.status = status | ||
| } | ||
| } | ||
|
|
||
| class EntityTooLarge extends ParseFailure { | ||
| constructor(limit: number) { | ||
| super(413, `request entity too large: ${limit}`, 'PayloadTooLargeError') | ||
| } | ||
| } | ||
|
|
||
| function fail(): never { | ||
| throw new ParseFailure(400, 'bad body', 'SyntaxError') | ||
| } | ||
|
|
||
| const describe = (err: unknown): string => (err instanceof Error ? `${err.name}: ${err.message}` : `other:${String(err)}`) | ||
|
|
||
| let caught: unknown = undefined | ||
| try { | ||
| fail() | ||
| } catch (err) { | ||
| caught = err | ||
| } | ||
|
|
||
| //! expect: true true SyntaxError: bad body SyntaxError: bad body | ||
| console.log(caught instanceof Error, caught instanceof ParseFailure, describe(caught), String(caught)) | ||
|
|
||
| const deep: unknown = new EntityTooLarge(1024) | ||
| //! expect: true true true PayloadTooLargeError: request entity too large: 1024 | ||
| console.log(deep instanceof Error, deep instanceof ParseFailure, deep instanceof EntityTooLarge, String(deep)) | ||
|
|
||
| const plain: unknown = new Error('plain') | ||
| const typed: unknown = new TypeError('typed') | ||
| const record: unknown = { name: 'Error', message: 'shaped like one' } | ||
| //! expect: Error: plain TypeError: typed false other:[object Object] | ||
| console.log(describe(plain), describe(typed), record instanceof Error, describe(record)) |
16 changes: 16 additions & 0 deletions
16
test/runtime/object-keys-over-recursive-dictionary-or-array-union.runtime.ts
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| type Leaf = string | ||
| type Tree = { [key: string]: Leaf | Tree | Branch } | ||
| type Branch = (Leaf | Tree | Branch | undefined)[] | ||
| type Container = Tree | Branch | ||
|
|
||
| const keysOf = (container: Container): string => Object.keys(container).join(',') | ||
|
|
||
| const tree: Tree = { a: 'x', b: ['y', { c: 'z' }] } | ||
| const branch: Branch = ['p', tree, undefined, 'q'] | ||
| tree['self'] = branch | ||
|
|
||
| console.log(keysOf(tree)) | ||
| console.log(keysOf(branch)) | ||
|
|
||
| //! expect: a,b,self | ||
| //! expect: 0,1,2,3 |
44 changes: 44 additions & 0 deletions
44
test/runtime/string-of-boxed-error-subclass-overriding-tostring.runtime.ts
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class HttpError extends Error { | ||
| status: number | ||
| constructor(status: number, message: string) { | ||
| super(message) | ||
| this.name = 'HttpError' | ||
| this.status = status | ||
| } | ||
| toString(): string { | ||
| return `HttpError(${this.status}): ${this.message}` | ||
| } | ||
| } | ||
|
|
||
| class NotFound extends HttpError { | ||
| constructor(path: string) { | ||
| super(404, `no route for ${path}`) | ||
| } | ||
| } | ||
|
|
||
| class PlainError extends Error { | ||
| constructor(message: string) { | ||
| super(message) | ||
| this.name = 'PlainError' | ||
| } | ||
| } | ||
|
|
||
| const overriding: unknown = new HttpError(500, 'boom') | ||
| const inherited: unknown = new NotFound('/missing') | ||
| const plain: unknown = new PlainError('plain') | ||
|
|
||
| //! expect: HttpError(500): boom HttpError(500): boom | ||
| console.log(String(overriding), `${overriding}`) | ||
| //! expect: HttpError(404): no route for /missing | ||
| console.log(String(inherited)) | ||
| //! expect: PlainError: plain | ||
| console.log(String(plain)) | ||
|
|
||
| let caught: unknown = undefined | ||
| try { | ||
| throw overriding | ||
| } catch (err) { | ||
| caught = err | ||
| } | ||
| //! expect: true caught HttpError(500): boom | ||
| console.log(caught instanceof HttpError, 'caught', String(caught)) |
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.