Skip to content

Commit

Permalink
Improve missingCaseError error message
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal committed Nov 12, 2020
1 parent 7d4d85a commit 947e9c3
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ts/util/missingCaseError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// Copyright 2018-2020 Signal Messenger, LLC
// 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
// a `switch` statement have been handled, e.g.
//
Expand All @@ -21,4 +32,4 @@
// handled by our `switch` / `case` statement which is useful for code
// maintenance and system evolution.
export const missingCaseError = (x: never): TypeError =>
new TypeError(`Unhandled case: ${x}`);
new TypeError(`Unhandled case: ${stringify(x)}`);

0 comments on commit 947e9c3

Please sign in to comment.