11import { expect } from '@jest/globals' ;
22import { SnapError } from '@metamask/snaps-sdk' ;
33
4- import { handle } from './errors' ;
4+ import { withCatchAndThrowSnapError } from './errors' ;
55import logger from './logger' ;
66
77// Mock the logger to avoid actual console output during tests
@@ -21,7 +21,7 @@ describe('errors', () => {
2121 const mockFn = jest . fn ( ) . mockResolvedValue ( 'success' ) ;
2222 const scope = 'test-scope' ;
2323
24- const result = await handle ( scope , mockFn ) ;
24+ const result = await withCatchAndThrowSnapError ( scope , mockFn ) ;
2525
2626 expect ( result ) . toBe ( 'success' ) ;
2727 expect ( mockFn ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -33,7 +33,9 @@ describe('errors', () => {
3333 const mockFn = jest . fn ( ) . mockRejectedValue ( originalError ) ;
3434 const scope = 'test-scope' ;
3535
36- await expect ( handle ( scope , mockFn ) ) . rejects . toThrow ( SnapError ) ;
36+ await expect ( withCatchAndThrowSnapError ( scope , mockFn ) ) . rejects . toThrow (
37+ SnapError ,
38+ ) ;
3739
3840 expect ( mockFn ) . toHaveBeenCalledTimes ( 1 ) ;
3941 expect ( mockLogger . error ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -45,7 +47,7 @@ describe('errors', () => {
4547 const scope = 'test-scope' ;
4648
4749 try {
48- await handle ( scope , mockFn ) ;
50+ await withCatchAndThrowSnapError ( scope , mockFn ) ;
4951 } catch ( error ) {
5052 // Expected to throw
5153 }
@@ -66,7 +68,9 @@ describe('errors', () => {
6668 const mockFn = jest . fn ( ) . mockRejectedValue ( nonErrorValue ) ;
6769 const scope = 'test-scope' ;
6870
69- await expect ( handle ( scope , mockFn ) ) . rejects . toThrow ( SnapError ) ;
71+ await expect ( withCatchAndThrowSnapError ( scope , mockFn ) ) . rejects . toThrow (
72+ SnapError ,
73+ ) ;
7074
7175 expect ( mockLogger . error ) . toHaveBeenCalledTimes ( 1 ) ;
7276 const logCall = mockLogger . error . mock . calls [ 0 ] ;
@@ -78,7 +82,9 @@ describe('errors', () => {
7882 const mockFn = jest . fn ( ) . mockRejectedValue ( null ) ;
7983 const scope = 'test-scope' ;
8084
81- await expect ( handle ( scope , mockFn ) ) . rejects . toThrow ( SnapError ) ;
85+ await expect ( withCatchAndThrowSnapError ( scope , mockFn ) ) . rejects . toThrow (
86+ SnapError ,
87+ ) ;
8288
8389 expect ( mockLogger . error ) . toHaveBeenCalledTimes ( 1 ) ;
8490 } ) ;
@@ -90,7 +96,7 @@ describe('errors', () => {
9096
9197 let caughtError : unknown ;
9298 try {
93- await handle ( scope , mockFn ) ;
99+ await withCatchAndThrowSnapError ( scope , mockFn ) ;
94100 } catch ( error ) {
95101 caughtError = error ;
96102 }
@@ -113,7 +119,7 @@ describe('errors', () => {
113119 const mockFn = jest . fn ( ) . mockResolvedValue ( testCase . value ) ;
114120 const scope = 'test-scope' ;
115121
116- const result = await handle ( scope , mockFn ) ;
122+ const result = await withCatchAndThrowSnapError ( scope , mockFn ) ;
117123
118124 expect ( result ) . toBe ( testCase . value ) ;
119125 expect ( mockLogger . error ) . not . toHaveBeenCalled ( ) ;
@@ -132,7 +138,9 @@ describe('errors', () => {
132138 const mockFn = jest . fn ( ) . mockRejectedValue ( errorType ) ;
133139 const scope = 'test-scope' ;
134140
135- await expect ( handle ( scope , mockFn ) ) . rejects . toThrow ( SnapError ) ;
141+ await expect ( withCatchAndThrowSnapError ( scope , mockFn ) ) . rejects . toThrow (
142+ SnapError ,
143+ ) ;
136144 }
137145
138146 expect ( mockLogger . error ) . toHaveBeenCalledTimes ( errorTypes . length ) ;
@@ -154,7 +162,7 @@ describe('errors', () => {
154162 const scope = 'test-scope' ;
155163
156164 try {
157- await handle ( scope , mockFn ) ;
165+ await withCatchAndThrowSnapError ( scope , mockFn ) ;
158166 } catch ( error ) {
159167 // Expected to throw
160168 }
@@ -170,7 +178,9 @@ describe('errors', () => {
170178 const mockFn = jest . fn ( ) . mockImplementation ( async ( ) => rejectedPromise ) ;
171179 const scope = 'test-scope' ;
172180
173- await expect ( handle ( scope , mockFn ) ) . rejects . toThrow ( SnapError ) ;
181+ await expect ( withCatchAndThrowSnapError ( scope , mockFn ) ) . rejects . toThrow (
182+ SnapError ,
183+ ) ;
174184
175185 expect ( mockLogger . error ) . toHaveBeenCalledTimes ( 1 ) ;
176186 } ) ;
0 commit comments