-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
110 additions
and
84 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { renderMessages } from './message' | ||
|
||
describe('message', () => { | ||
|
||
it('should print errors', () => { | ||
expect(renderMessages(['Message', new Error('Hello')], { trace: false })).toMatchInlineSnapshot(`"Message Error: Hello"`) | ||
|
||
expect(renderMessages(['Message', new Error('Hello')], { trace: false }),).toMatchInlineSnapshot(`"Message Error: Hello"`) | ||
|
||
expect( | ||
renderMessages([ | ||
'Message', | ||
{ a: 1, b: null }, | ||
null, | ||
undefined, | ||
Number.NaN, | ||
1e2, | ||
'string', | ||
], { trace: false }), | ||
).toMatchInlineSnapshot(` | ||
"Message { | ||
"a": 1, | ||
"b": null | ||
} null undefined NaN 100 string" | ||
`) | ||
|
||
// it('should handle promise rejection', async () => { | ||
// let promise = new Promise((_, reject) => reject(new Error('Hello'))) | ||
// let myRejectionEvent = new PromiseRejectionEvent("unhandledrejection", { | ||
// promise, | ||
// reason: "My house is on fire", | ||
// }) | ||
|
||
// expect(renderMessages(myRejectionEvent)).toMatchInlineSnapshot() | ||
|
||
}) | ||
|
||
}) |
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,48 @@ | ||
import { Uint8ArrayToHexDump } from "./bin" | ||
import { isArray } from "./is" | ||
import { objectPlain } from "./object" | ||
|
||
export interface RenderMessagesOptions { | ||
trace?: boolean // = true | ||
pretty?: boolean // = true | ||
} | ||
|
||
export function formatMessages( | ||
messages: any[], | ||
opt: RenderMessagesOptions = {}, | ||
): any[] { | ||
const { trace = true, pretty = true } = opt | ||
return messages.map((obj) => { | ||
if (obj && typeof obj === 'object') { | ||
if (pretty && (obj instanceof Uint8Array || obj instanceof ArrayBuffer)) | ||
return `\n${Uint8ArrayToHexDump(obj)}\n` | ||
|
||
if (obj instanceof Error) { | ||
return `${obj.name}: ${obj.message}` + (trace ? `\n${obj.stack}` : '') | ||
} | ||
if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) { | ||
return `${obj.error.name || 'ErrorEvent'}: ${obj.error.message}` + (trace ? `\n${obj.error.stack}` : '') | ||
} | ||
if (typeof DOMException !== 'undefined' && obj instanceof DOMException) { | ||
return `${obj.name || 'DOMException'}: ${obj.message}` + (trace ? `\n${obj.stack}` : '') | ||
} | ||
if (obj && typeof obj === 'object' && 'reason' in obj) { | ||
return `PromiseRejection ${obj.type}: ${obj.reason}` + (trace ? `\n${obj.stack}` : '') | ||
} | ||
|
||
try { | ||
obj = objectPlain(obj) | ||
return pretty ? JSON.stringify(obj, null, 2) : JSON.stringify(obj) | ||
} | ||
catch (err) {} | ||
} | ||
return String(obj) | ||
}) | ||
} | ||
|
||
export function renderMessages( | ||
messages: any | any[], | ||
opt: RenderMessagesOptions = {}, | ||
): string { | ||
return formatMessages(isArray(messages) ? messages : [messages], opt).join(' ') | ||
} |
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
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