Improve missingCaseError error message
This commit is contained in:
parent
7d4d85a755
commit
947e9c366c
1 changed files with 12 additions and 1 deletions
|
@ -1,6 +1,17 @@
|
||||||
// Copyright 2018-2020 Signal Messenger, LLC
|
// Copyright 2018-2020 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
const stringify = (value: unknown): string => {
|
||||||
|
try {
|
||||||
|
// `JSON.stringify` can return `undefined` (TypeScript has incorrect types here).
|
||||||
|
// However, this is fine because we interpolate it into a string, so it shows up as
|
||||||
|
// "undefined" in the final error message.
|
||||||
|
return JSON.stringify(value);
|
||||||
|
} catch (err) {
|
||||||
|
return Object.prototype.toString.call(value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// `missingCaseError` is useful for compile-time checking that all `case`s in
|
// `missingCaseError` is useful for compile-time checking that all `case`s in
|
||||||
// a `switch` statement have been handled, e.g.
|
// a `switch` statement have been handled, e.g.
|
||||||
//
|
//
|
||||||
|
@ -21,4 +32,4 @@
|
||||||
// handled by our `switch` / `case` statement which is useful for code
|
// handled by our `switch` / `case` statement which is useful for code
|
||||||
// maintenance and system evolution.
|
// maintenance and system evolution.
|
||||||
export const missingCaseError = (x: never): TypeError =>
|
export const missingCaseError = (x: never): TypeError =>
|
||||||
new TypeError(`Unhandled case: ${x}`);
|
new TypeError(`Unhandled case: ${stringify(x)}`);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue