generated from MetaMask/template-snap-monorepo
-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: integrate error tracking service with RPC handlers #242
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
mj-kiwi
wants to merge
31
commits into
main
Choose a base branch
from
feat/error-tracking
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.
+587
−37
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
da93dcc
feat: integrate error tracking service with RPC handlers
mj-kiwi 492d4ab
Merge branch 'main' into feat/error-tracking
mj-kiwi c5f08dc
fix: correct indentation for i18n initialization in onRpcRequest handler
mj-kiwi a3f3f56
Merge branch 'main' into feat/error-tracking
mj-kiwi 244595a
feat: integrate error tracking functionality across RPC handlers
mj-kiwi e97b037
refactor: remove rpcErrorHandler files and clean up package.json exports
mj-kiwi a3a5fcf
feat: add unit tests for SnapErrorTracker functionality and behavior
mj-kiwi 797c47a
Merge branch 'main' into feat/error-tracking
mj-kiwi 876aa4a
fix: update import statement for ErrorTrackingConfig to use type import
mj-kiwi 0941e99
refactor: update shouldTrackError to use explicit boolean return type…
mj-kiwi c997296
refactor: enhance error handling in SnapErrorTracker and simplify ins…
mj-kiwi d4d7517
Merge branch 'main' into feat/error-tracking
mj-kiwi 632e27a
Merge branch 'main' into feat/error-tracking
mj-kiwi b299440
feat: enhance error tracking by including request parameters in error…
mj-kiwi a7a9ba7
test: enhance error tracking tests to handle empty error messages
mj-kiwi 1d5c498
Merge branch 'main' into feat/error-tracking
mj-kiwi 8a145a4
Merge branch 'main' into feat/error-tracking
mj-kiwi 3be17e2
fix: improve error tracking logic to handle null and undefined error …
mj-kiwi 372f177
refactor: remove setEnabled and isEnabled methods from SnapErrorTrack…
mj-kiwi ae6d406
refactor: change #enabled property to readonly in SnapErrorTracker class
mj-kiwi 633037f
fix: reorder processing lock checks in onRpcRequest to improve error …
mj-kiwi f6e6868
refactor: update error tracking implementation to use snapProvider an…
mj-kiwi cd4d408
refactor: update error capturing to use structured error objects and …
mj-kiwi 358da1c
test: add case to track string errors with default filter in SnapErro…
mj-kiwi 776130c
refactor: use optional chaining for request properties in error tracking
mj-kiwi 515144a
refactor: replace console.warn with logger.warn for error tracking
mj-kiwi 626edba
refactor: streamline logger warning spy implementation in error track…
mj-kiwi 642ea4f
refactor: update comment to clarify lock token creation in onRpcReque…
mj-kiwi 9fde26f
refactor: enhance error handling to preserve falsy message values for…
mj-kiwi 98f9432
refactor: improve error tracking by releasing lock without awaiting c…
mj-kiwi e6cc6b0
refactor: enhance error tracking by allowing captureError to handle p…
mj-kiwi 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
Some comments aren't visible on the classic Files Changed page.
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
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
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,203 @@ | ||
| import { getJsonError } from '@metamask/snaps-sdk'; | ||
|
|
||
| import { logger } from './logger'; | ||
|
|
||
| /** | ||
| * Error information that will be tracked. | ||
| */ | ||
| export type ErrorTrackingInfo = { | ||
| snapName: string; | ||
| method: string; | ||
| url?: string | undefined; | ||
| statusCode?: number | undefined; | ||
| errorMessage: string; | ||
| errorStack?: string | undefined; | ||
| responseData?: any | undefined; | ||
| requestParams?: any | undefined; | ||
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Error tracking service configuration. | ||
| */ | ||
| export type ErrorTrackingConfig = { | ||
| enabled: boolean; | ||
| snapName: string; | ||
| snapProvider: any; | ||
| shouldTrackError?: (error: any) => boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Error tracking service for snaps. | ||
| * Provides a unified way to track and report errors across different snaps. | ||
| */ | ||
| export class SnapErrorTracker { | ||
| readonly #enabled: boolean; | ||
|
|
||
| readonly #snapName: string; | ||
|
|
||
| readonly #snapProvider: any; | ||
|
|
||
| readonly #shouldTrackError: (error: any) => boolean; | ||
|
|
||
| constructor(config: ErrorTrackingConfig) { | ||
| this.#enabled = config.enabled; | ||
| this.#snapName = config.snapName; | ||
| this.#snapProvider = config.snapProvider; | ||
| this.#shouldTrackError = | ||
| config.shouldTrackError ?? | ||
| ((error: any): boolean => { | ||
| // By default, track Error instances, string errors, or objects with error-like properties | ||
| return ( | ||
| error instanceof Error || | ||
| typeof error === 'string' || | ||
| (typeof error === 'object' && | ||
| error !== null && | ||
| (error?.message !== undefined || error?.error !== undefined)) | ||
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts error information from various error formats. | ||
| * | ||
| * @param error - The error to extract information from. | ||
| * @param method - The method/operation that triggered the error. | ||
| * @param requestParams - Optional request parameters that triggered the error. | ||
| * @returns The extracted error information. | ||
| */ | ||
| #extractErrorInfo( | ||
| error: any, | ||
| method: string, | ||
| requestParams?: any, | ||
| ): ErrorTrackingInfo { | ||
| const errorInfo: ErrorTrackingInfo = { | ||
| snapName: this.#snapName, | ||
| method, | ||
| errorMessage: 'Unknown error', | ||
| }; | ||
|
|
||
| // Check if the error has a currentUrl property | ||
| if (error?.currentUrl) { | ||
| errorInfo.url = error.currentUrl; | ||
| } | ||
|
|
||
| // Handle different error formats (align with shouldTrackError: use !== undefined so | ||
| // falsy values like '' or null are preserved for Sentry instead of "Unknown error") | ||
| if (error instanceof Error) { | ||
| errorInfo.errorMessage = error.message; | ||
| errorInfo.errorStack = error.stack; | ||
| } else if (typeof error === 'string') { | ||
| errorInfo.errorMessage = error; | ||
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if ( | ||
| typeof error === 'object' && | ||
| error !== null && | ||
| 'message' in error && | ||
| error.message !== undefined | ||
| ) { | ||
| errorInfo.errorMessage = String(error.message); | ||
| } else if ( | ||
| typeof error === 'object' && | ||
| error !== null && | ||
| 'error' in error && | ||
| error.error !== undefined | ||
| ) { | ||
| if (typeof error.error === 'string') { | ||
| errorInfo.errorMessage = error.error; | ||
| } else { | ||
| try { | ||
| errorInfo.errorMessage = JSON.stringify(error.error); | ||
| } catch { | ||
| errorInfo.errorMessage = String(error.error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Get status code if available | ||
| if (error?.status) { | ||
| errorInfo.statusCode = error.status; | ||
| } else if (error?.statusCode) { | ||
| errorInfo.statusCode = error.statusCode; | ||
| } | ||
|
|
||
| // Get response data if available | ||
| if (error?.response) { | ||
| errorInfo.responseData = error.response; | ||
| } else if (error?.data) { | ||
| errorInfo.responseData = error.data; | ||
| } | ||
|
|
||
| // Get request params if available (from parameter or error object) | ||
| if (requestParams !== undefined) { | ||
| errorInfo.requestParams = requestParams; | ||
| } else if (error?.requestParams) { | ||
| errorInfo.requestParams = error.requestParams; | ||
| } else if (error?.params) { | ||
| errorInfo.requestParams = error.params; | ||
| } | ||
|
|
||
| return errorInfo; | ||
| } | ||
|
|
||
| /** | ||
| * Tracks an error using the snap's error tracking mechanism. | ||
| * This function safely handles error tracking without throwing additional errors. | ||
| * | ||
| * @param errorInfo - The error information to track. | ||
| */ | ||
| async #trackErrorViaSnap(errorInfo: ErrorTrackingInfo): Promise<void> { | ||
| try { | ||
| // Use snap_trackError if available | ||
| if (this.#snapProvider?.request) { | ||
| await this.#snapProvider.request({ | ||
| method: 'snap_trackError', | ||
| params: { | ||
| error: getJsonError(new Error(JSON.stringify(errorInfo))), | ||
| }, | ||
| }); | ||
| } | ||
| } catch (trackingError) { | ||
| // Silently fail - don't let error tracking itself break the app | ||
| logger.warn( | ||
| '[SnapErrorTracker] Failed to track error via snap:', | ||
| trackingError, | ||
| ); | ||
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Captures and tracks an error. | ||
| * | ||
| * @param errorData - The error data object containing error, method, and optional requestParams. | ||
| * @param errorData.error - The error to capture. | ||
| * @param errorData.method - The method/operation name. | ||
| * @param errorData.requestParams - Optional request parameters that triggered the error. | ||
| */ | ||
| async captureError({ | ||
| error, | ||
| method, | ||
| requestParams, | ||
| }: { | ||
| error: any; | ||
| method: string; | ||
| requestParams?: any; | ||
| }): Promise<void> { | ||
| if (!this.#enabled || !this.#shouldTrackError(error)) { | ||
| return; | ||
| } | ||
|
|
||
| const errorInfo = this.#extractErrorInfo(error, method, requestParams); | ||
| await this.#trackErrorViaSnap(errorInfo); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates an error tracker instance. | ||
| * | ||
| * @param config - Configuration for the error tracker. | ||
| * @returns The error tracker instance. | ||
| */ | ||
| export function createErrorTracker( | ||
mj-kiwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| config: ErrorTrackingConfig, | ||
| ): SnapErrorTracker { | ||
| return new SnapErrorTracker(config); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,3 +1,4 @@ | ||
| export * from './common'; | ||
| export * from './error'; | ||
| export * from './logger'; | ||
| export * from './errorTracking'; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.