-
Notifications
You must be signed in to change notification settings - Fork 22
Standardize error handling with shared utility #434
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93fcde7
Standardize error handling with shared getErrorMessage utility
markvp a8e0875
Address review: enhance getErrorMessage with string and Error-like ha…
markvp 218d52a
Address review: handle non-string message, nested Error, and object s…
markvp 6b34bab
Add unit tests for getErrorMessage utility
markvp 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
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
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,27 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025-2026 Open Home Foundation | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Extracts an error message from an unknown error value. | ||
| * Handles string errors, Error instances, Error-like objects, and | ||
| * falls back to JSON serialization or String() for other types. | ||
| */ | ||
| export function getErrorMessage(err: unknown): string { | ||
| if (typeof err === "string") return err; | ||
| if (err instanceof Error) return err.message; | ||
| if (err !== null && typeof err === "object") { | ||
| const anyErr = err as { message?: unknown }; | ||
| if (typeof anyErr.message === "string") return anyErr.message; | ||
|
markvp marked this conversation as resolved.
|
||
| if (anyErr.message instanceof Error) return anyErr.message.message; | ||
| if (anyErr.message != null) return String(anyErr.message); | ||
| try { | ||
| return JSON.stringify(err); | ||
| } catch { | ||
| return Object.prototype.toString.call(err); | ||
| } | ||
| } | ||
| return String(err); | ||
| } | ||
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,18 @@ | ||
| { | ||
| "extends": "../../tools/tsc/tsconfig.test.json", | ||
| "compilerOptions": { | ||
| "types": [ | ||
| "node", | ||
| "mocha", | ||
| "@matter/testing" | ||
| ] | ||
| }, | ||
| "references": [ | ||
| { | ||
| "path": "../../tools/src" | ||
| }, | ||
| { | ||
| "path": "../src" | ||
| } | ||
| ] | ||
| } |
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,71 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025-2026 Open Home Foundation | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { getErrorMessage } from "../../src/util/errorUtils.js"; | ||
|
|
||
| describe("getErrorMessage", () => { | ||
| it("returns the string directly for string errors", () => { | ||
| expect(getErrorMessage("something went wrong")).to.equal("something went wrong"); | ||
| }); | ||
|
|
||
| it("returns empty string for empty string errors", () => { | ||
| expect(getErrorMessage("")).to.equal(""); | ||
| }); | ||
|
|
||
| it("returns message from Error instances", () => { | ||
| expect(getErrorMessage(new Error("test error"))).to.equal("test error"); | ||
| }); | ||
|
|
||
| it("returns message from Error subclasses", () => { | ||
| expect(getErrorMessage(new TypeError("type error"))).to.equal("type error"); | ||
| expect(getErrorMessage(new RangeError("range error"))).to.equal("range error"); | ||
| }); | ||
|
|
||
| it("returns message from Error-like objects with string message", () => { | ||
| expect(getErrorMessage({ message: "error-like" })).to.equal("error-like"); | ||
| }); | ||
|
|
||
| it("returns nested message from Error-like objects with Error message", () => { | ||
| expect(getErrorMessage({ message: new Error("nested") })).to.equal("nested"); | ||
| }); | ||
|
|
||
| it("returns stringified message for non-string, non-Error message properties", () => { | ||
| expect(getErrorMessage({ message: 42 })).to.equal("42"); | ||
| expect(getErrorMessage({ message: true })).to.equal("true"); | ||
| }); | ||
|
|
||
| it("returns JSON stringified result for objects without message", () => { | ||
| expect(getErrorMessage({ code: 404, detail: "not found" })).to.equal( | ||
| '{"code":404,"detail":"not found"}', | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to Object.prototype.toString for non-serializable objects", () => { | ||
| const circular: Record<string, unknown> = {}; | ||
| circular.self = circular; | ||
| expect(getErrorMessage(circular)).to.equal("[object Object]"); | ||
| }); | ||
|
|
||
| it("uses String() for numbers", () => { | ||
| expect(getErrorMessage(42)).to.equal("42"); | ||
| expect(getErrorMessage(0)).to.equal("0"); | ||
| expect(getErrorMessage(NaN)).to.equal("NaN"); | ||
| }); | ||
|
|
||
| it("uses String() for booleans", () => { | ||
| expect(getErrorMessage(true)).to.equal("true"); | ||
| expect(getErrorMessage(false)).to.equal("false"); | ||
| }); | ||
|
|
||
| it("uses String() for null and undefined", () => { | ||
| expect(getErrorMessage(null)).to.equal("null"); | ||
| expect(getErrorMessage(undefined)).to.equal("undefined"); | ||
| }); | ||
|
|
||
| it("handles Symbol values", () => { | ||
| expect(getErrorMessage(Symbol("test"))).to.equal("Symbol(test)"); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "compilerOptions": { "composite": true }, | ||
| "files": [], | ||
| "references": [{ "path": "src" }] | ||
| "references": [{ "path": "src" }, { "path": "test" }] | ||
| } |
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.