-
Notifications
You must be signed in to change notification settings - Fork 34
fix(wallets): log WalletNotAvailableError at warn instead of error #1846
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
Open
devin-ai-integration
wants to merge
3
commits into
main
Choose a base branch
from
devin/1779181695-fix-wallet-not-found-log-level
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
426c882
fix(wallets): log WalletNotAvailableError at warn instead of error
devin-ai-integration[bot] b089205
chore: add changeset for expected-errors log level fix
devin-ai-integration[bot] 224b979
test(logger): add unit tests for expectedErrors in WithLoggerContext …
devin-ai-integration[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@crossmint/common-sdk-base": patch | ||
| "@crossmint/wallets-sdk": patch | ||
| --- | ||
|
|
||
| Log `WalletNotAvailableError` from `walletFactory.getWallet` at warn level instead of error. The `WithLoggerContext` decorator now supports an `expectedErrors` option so decorated methods can declare which error classes represent normal business outcomes (e.g. wallet not found) that should not pollute error-level monitoring. |
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,143 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { WithLoggerContext } from "./decorators"; | ||
| import { SdkLogger } from "./SdkLogger"; | ||
|
|
||
| class ExpectedError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = "ExpectedError"; | ||
| } | ||
| } | ||
|
|
||
| class UnexpectedError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = "UnexpectedError"; | ||
| } | ||
| } | ||
|
|
||
| function createMockLogger() { | ||
| const logger = new SdkLogger({ packageName: "test" }); | ||
| logger.warn = vi.fn(); | ||
| logger.error = vi.fn(); | ||
| logger.info = vi.fn(); | ||
| logger.debug = vi.fn(); | ||
| return logger; | ||
| } | ||
|
|
||
| describe("WithLoggerContext", () => { | ||
| describe("expectedErrors", () => { | ||
| it("logs expected errors at warn level", async () => { | ||
| const logger = createMockLogger(); | ||
|
|
||
| class Subject { | ||
| @WithLoggerContext({ | ||
| logger, | ||
| methodName: "subject.method", | ||
| expectedErrors: [ExpectedError], | ||
| }) | ||
| async doWork(): Promise<void> { | ||
| throw new ExpectedError("not found"); | ||
| } | ||
| } | ||
|
|
||
| const subject = new Subject(); | ||
| await expect(subject.doWork()).rejects.toThrow("not found"); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith("subject.method threw an error", { | ||
| error: expect.any(ExpectedError), | ||
| }); | ||
| expect(logger.error).not.toHaveBeenCalledWith("subject.method threw an error", expect.anything()); | ||
| }); | ||
|
|
||
| it("logs unexpected errors at error level", async () => { | ||
| const logger = createMockLogger(); | ||
|
|
||
| class Subject { | ||
| @WithLoggerContext({ | ||
| logger, | ||
| methodName: "subject.method", | ||
| expectedErrors: [ExpectedError], | ||
| }) | ||
| async doWork(): Promise<void> { | ||
| throw new UnexpectedError("boom"); | ||
| } | ||
| } | ||
|
|
||
| const subject = new Subject(); | ||
| await expect(subject.doWork()).rejects.toThrow("boom"); | ||
|
|
||
| expect(logger.error).toHaveBeenCalledWith("subject.method threw an error", { | ||
| error: expect.any(UnexpectedError), | ||
| }); | ||
| expect(logger.warn).not.toHaveBeenCalledWith("subject.method threw an error", expect.anything()); | ||
| }); | ||
|
|
||
| it("logs all errors at error level when expectedErrors is omitted", async () => { | ||
| const logger = createMockLogger(); | ||
|
|
||
| class Subject { | ||
| @WithLoggerContext({ | ||
| logger, | ||
| methodName: "subject.method", | ||
| }) | ||
| async doWork(): Promise<void> { | ||
| throw new ExpectedError("not found"); | ||
| } | ||
| } | ||
|
|
||
| const subject = new Subject(); | ||
| await expect(subject.doWork()).rejects.toThrow("not found"); | ||
|
|
||
| expect(logger.error).toHaveBeenCalledWith("subject.method threw an error", { | ||
| error: expect.any(ExpectedError), | ||
| }); | ||
| expect(logger.warn).not.toHaveBeenCalledWith("subject.method threw an error", expect.anything()); | ||
| }); | ||
|
|
||
| it("still rethrows the error in all cases", async () => { | ||
| const logger = createMockLogger(); | ||
|
|
||
| class Subject { | ||
| @WithLoggerContext({ | ||
| logger, | ||
| methodName: "subject.method", | ||
| expectedErrors: [ExpectedError], | ||
| }) | ||
| async doWork(err: Error): Promise<void> { | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| const subject = new Subject(); | ||
| const expected = new ExpectedError("expected"); | ||
| const unexpected = new UnexpectedError("unexpected"); | ||
|
|
||
| await expect(subject.doWork(expected)).rejects.toBe(expected); | ||
| await expect(subject.doWork(unexpected)).rejects.toBe(unexpected); | ||
| }); | ||
|
|
||
| it("handles sync methods that throw expected errors", () => { | ||
| const logger = createMockLogger(); | ||
|
|
||
| class Subject { | ||
| @WithLoggerContext({ | ||
| logger, | ||
| methodName: "subject.syncMethod", | ||
| expectedErrors: [ExpectedError], | ||
| }) | ||
| doWorkSync(): void { | ||
| throw new ExpectedError("sync not found"); | ||
| } | ||
| } | ||
|
|
||
| const subject = new Subject(); | ||
| expect(() => subject.doWorkSync()).toThrow("sync not found"); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith("subject.syncMethod threw an error", { | ||
| error: expect.any(ExpectedError), | ||
| }); | ||
| expect(logger.error).not.toHaveBeenCalledWith("subject.syncMethod threw an error", expect.anything()); | ||
| }); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectedErrorsbranchThe
logThrownErrorhelper introduces a new code path (warn vs. error selection) that has no test coverage — there are currently no test files underpackages/common/base/src/logger/. A test verifying that an expected-error class triggerslogger.warnwhile an unexpected error triggerslogger.error(and that the error is still re-thrown in both cases) would prevent silent regressions to this decorator, which is shared across the SDK.Rule Used: Add unit tests when implementing new validation lo... (source)
Learned From
Paella-Labs/crossbit-main#21014
Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, you sneaky little gecko 🐍 — adding tests now for the
expectedErrorsbranch. Will push shortly.