generated from MetaMask/template-snap-monorepo
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Merged
Merged
Changes from 5 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
67 changes: 67 additions & 0 deletions
67
packages/gator-permissions-snap/src/rpc/rpcErrorHandler.ts
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,67 @@ | ||
| /** | ||
| * RPC Error Handler Wrapper | ||
| * | ||
| * This module demonstrates how to integrate the error tracking service | ||
| * with RPC request handlers to capture and monitor errors in snap operations. | ||
| */ | ||
|
|
||
| import { getErrorTracker } from '@metamask/7715-permissions-shared/utils'; | ||
| import type { Json, JsonRpcParams } from '@metamask/snaps-sdk'; | ||
|
|
||
| type RpcHandler = (params?: JsonRpcParams) => Promise<Json>; | ||
|
|
||
| /** | ||
| * Wraps an RPC handler with error tracking. | ||
| * Captures any errors that occur during RPC execution while maintaining | ||
| * the original error flow. | ||
| * | ||
| * @param handler - The original RPC handler function. | ||
| * @param methodName - The RPC method name (for error tracking). | ||
| * @returns A wrapped RPC handler with error tracking. | ||
| */ | ||
| export function wrapRpcHandlerWithErrorTracking( | ||
| handler: RpcHandler, | ||
| methodName: string, | ||
| ): RpcHandler { | ||
| return async (params?: JsonRpcParams): Promise<Json> => { | ||
| const errorTracker = getErrorTracker(); | ||
|
|
||
| try { | ||
| const result = await handler(params); | ||
|
|
||
| // Check if response indicates an error (even with 200 status) | ||
| if ( | ||
| result && | ||
| typeof result === 'object' && | ||
| 'error' in result && | ||
| (result as any).error | ||
| ) { | ||
| await errorTracker.captureResponseError(result, methodName); | ||
| } | ||
|
|
||
| return result; | ||
| } catch (error) { | ||
| await errorTracker.captureError(error, methodName); | ||
| throw error; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Higher-order function to create multiple wrapped RPC handlers at once. | ||
| * Useful for wrapping all RPC handlers in a batch operation. | ||
| * | ||
| * @param handlers - An object mapping method names to RPC handler functions. | ||
| * @returns An object with wrapped handlers. | ||
| */ | ||
| export function wrapRpcHandlersWithErrorTracking( | ||
| handlers: Record<string, RpcHandler>, | ||
| ): Record<string, RpcHandler> { | ||
| const wrapped: Record<string, RpcHandler> = {}; | ||
|
|
||
| for (const [methodName, handler] of Object.entries(handlers)) { | ||
| wrapped[methodName] = wrapRpcHandlerWithErrorTracking(handler, methodName); | ||
| } | ||
|
|
||
| return wrapped; | ||
| } |
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
72 changes: 72 additions & 0 deletions
72
packages/permissions-kernel-snap/src/rpc/rpcErrorHandler.ts
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,72 @@ | ||
| /** | ||
| * RPC Error Handler Wrapper | ||
| * | ||
| * This module demonstrates how to integrate the error tracking service | ||
| * with RPC request handlers to capture and monitor errors in snap operations. | ||
| */ | ||
|
|
||
| import { getErrorTracker } from '@metamask/7715-permissions-shared/utils'; | ||
| import type { Json, JsonRpcParams } from '@metamask/snaps-sdk'; | ||
|
|
||
| type RpcHandlerOptions = { | ||
| siteOrigin: string; | ||
| params?: JsonRpcParams; | ||
| }; | ||
|
|
||
| type RpcHandler = (options: RpcHandlerOptions) => Promise<Json>; | ||
|
|
||
| /** | ||
| * Wraps an RPC handler with error tracking. | ||
| * Captures any errors that occur during RPC execution while maintaining | ||
| * the original error flow. | ||
| * | ||
| * @param handler - The original RPC handler function. | ||
| * @param methodName - The RPC method name (for error tracking). | ||
| * @returns A wrapped RPC handler with error tracking. | ||
| */ | ||
| export function wrapRpcHandlerWithErrorTracking( | ||
| handler: RpcHandler, | ||
| methodName: string, | ||
| ): RpcHandler { | ||
| return async (options: RpcHandlerOptions): Promise<Json> => { | ||
| const errorTracker = getErrorTracker(); | ||
|
|
||
| try { | ||
| const result = await handler(options); | ||
|
|
||
| // Check if response indicates an error (even with 200 status) | ||
| if ( | ||
| result && | ||
| typeof result === 'object' && | ||
| 'error' in result && | ||
| (result as any).error | ||
| ) { | ||
| await errorTracker.captureResponseError(result, methodName); | ||
| } | ||
|
|
||
| return result; | ||
| } catch (error) { | ||
| await errorTracker.captureError(error, methodName); | ||
| throw error; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Higher-order function to create multiple wrapped RPC handlers at once. | ||
| * Useful for wrapping all RPC handlers in a batch operation. | ||
| * | ||
| * @param handlers - An object mapping method names to RPC handler functions. | ||
| * @returns An object with wrapped handlers. | ||
| */ | ||
| export function wrapRpcHandlersWithErrorTracking( | ||
| handlers: Record<string, RpcHandler>, | ||
| ): Record<string, RpcHandler> { | ||
| const wrapped: Record<string, RpcHandler> = {}; | ||
|
|
||
| for (const [methodName, handler] of Object.entries(handlers)) { | ||
| wrapped[methodName] = wrapRpcHandlerWithErrorTracking(handler, methodName); | ||
| } | ||
|
|
||
| return wrapped; | ||
| } |
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.
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.