|
| 1 | +# `@metamask/error-reporting-service` |
| 2 | + |
| 3 | +Reports errors to an external app such as Sentry but in an agnostic fashion. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +`yarn add @metamask/error-reporting-service` |
| 8 | + |
| 9 | +or |
| 10 | + |
| 11 | +`npm install @metamask/error-reporting-service` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +This package is designed to be used in another module via a messenger, but can also be used on its own if needed. |
| 16 | + |
| 17 | +### Using the service via a messenger |
| 18 | + |
| 19 | +In most cases, you will want to use the error reporting service in your module via a messenger object. |
| 20 | + |
| 21 | +In this example, we have a controller, and something bad happens, but we want to report an error instead of throwing it. |
| 22 | + |
| 23 | +#### 1. Controller file |
| 24 | + |
| 25 | +```typescript |
| 26 | +// We need to get the type for the `ErrorReportingService:captureException` |
| 27 | +// action. |
| 28 | +import type { ErrorReportingServiceCaptureExceptionAction } from '@metamask/error-reporting-service'; |
| 29 | + |
| 30 | +// Now let's set up our controller, starting with the messenger. |
| 31 | +// Note that we grant the `ErrorReportingService:captureException` action to the |
| 32 | +// messenger. |
| 33 | +type AllowedActions = ErrorReportingServiceCaptureExceptionAction; |
| 34 | +type ExampleControllerMessenger = RestrictedMessenger< |
| 35 | + 'ExampleController', |
| 36 | + AllowedActions, |
| 37 | + never, |
| 38 | + AllowedActions['type'], |
| 39 | + never |
| 40 | +>; |
| 41 | + |
| 42 | +// Finally, we define our controller. |
| 43 | +class ExampleController extends BaseController< |
| 44 | + 'ExampleController', |
| 45 | + ExampleControllerState, |
| 46 | + ExampleControllerMessenger |
| 47 | +> { |
| 48 | + doSomething() { |
| 49 | + // Now imagine that we do something that produces an error and we want to |
| 50 | + // report the error. |
| 51 | + this.messagingSystem.call( |
| 52 | + 'ErrorReportingService:captureException', |
| 53 | + new Error('Something went wrong'), |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +#### 2A. Initialization file (browser) |
| 60 | + |
| 61 | +```typescript |
| 62 | +// We need a version of `captureException` from somewhere. Here, we are getting |
| 63 | +// it from `@sentry/browser`. |
| 64 | +import { captureException } from '@sentry/browser'; |
| 65 | + |
| 66 | +// We also need to get the ErrorReportingService. |
| 67 | +import { ErrorReportingService } from '@metamask/error-reporting-service'; |
| 68 | + |
| 69 | +// And we need our controller. |
| 70 | +import { ExampleController } from './example-controller'; |
| 71 | + |
| 72 | +// We need to have a global messenger. |
| 73 | +const globalMessenger = new Messenger(); |
| 74 | + |
| 75 | +// We need to create a restricted messenger for the ErrorReportingService, and |
| 76 | +// then we can create the service itself. |
| 77 | +const errorReportingServiceMessenger = globalMessenger.getRestricted({ |
| 78 | + allowedActions: [], |
| 79 | + allowedEvents: [], |
| 80 | +}); |
| 81 | +const errorReportingService = new ErrorReportingService({ |
| 82 | + messenger: errorReportingServiceMessenger, |
| 83 | + captureException, |
| 84 | +}); |
| 85 | + |
| 86 | +// Now we can create a restricted messenger for our controller, and then |
| 87 | +// we can create the controller too. |
| 88 | +// Note that we grant the `ErrorReportingService:captureException` action to the |
| 89 | +// messenger. |
| 90 | +const exampleControllerMessenger = globalMessenger.getRestricted({ |
| 91 | + allowedActions: ['ErrorReportingService:captureException'], |
| 92 | + allowedEvents: [], |
| 93 | +}); |
| 94 | +const exampleController = new ExampleController({ |
| 95 | + messenger: exampleControllerMessenger, |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +#### 2B. Initialization file (React Native) |
| 100 | + |
| 101 | +```typescript |
| 102 | +// We need a version of `captureException` from somewhere. Here, we are getting |
| 103 | +// it from `@sentry/react-native`. |
| 104 | +import { captureException } from '@sentry/react-native'; |
| 105 | + |
| 106 | +// We also need to get the ErrorReportingService. |
| 107 | +import { ErrorReportingService } from '@metamask/error-reporting-service'; |
| 108 | + |
| 109 | +// And we need our controller. |
| 110 | +import { ExampleController } from './example-controller'; |
| 111 | + |
| 112 | +// We need to have a global messenger. |
| 113 | +const globalMessenger = new Messenger(); |
| 114 | + |
| 115 | +// We need to create a restricted messenger for the ErrorReportingService, and |
| 116 | +// then we can create the service itself. |
| 117 | +const errorReportingServiceMessenger = globalMessenger.getRestricted({ |
| 118 | + allowedActions: [], |
| 119 | + allowedEvents: [], |
| 120 | +}); |
| 121 | +const errorReportingService = new ErrorReportingService({ |
| 122 | + messenger: errorReportingServiceMessenger, |
| 123 | + captureException, |
| 124 | +}); |
| 125 | + |
| 126 | +// Now we can create a restricted messenger for our controller, and then |
| 127 | +// we can create the controller too. |
| 128 | +// Note that we grant the `ErrorReportingService:captureException` action to the |
| 129 | +// messenger. |
| 130 | +const exampleControllerMessenger = globalMessenger.getRestricted({ |
| 131 | + allowedActions: ['ErrorReportingService:captureException'], |
| 132 | + allowedEvents: [], |
| 133 | +}); |
| 134 | +const exampleController = new ExampleController({ |
| 135 | + messenger: exampleControllerMessenger, |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +#### 3. Using the controller |
| 140 | + |
| 141 | +```typescript |
| 142 | +// Now this will report an error without throwing it. |
| 143 | +exampleController.doSomething(); |
| 144 | +``` |
| 145 | + |
| 146 | +### Using the service directly |
| 147 | + |
| 148 | +You probably don't need to use the service directly, but if you do, here's how. |
| 149 | + |
| 150 | +In this example, we have a function, and we use the error reporting service there. |
| 151 | + |
| 152 | +#### 1. Function file |
| 153 | + |
| 154 | +```typescript |
| 155 | +export function doSomething( |
| 156 | + errorReportingService: AbstractErrorReportingService, |
| 157 | +) { |
| 158 | + errorReportingService.captureException(new Error('Something went wrong')); |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +#### 2A. Calling file (browser) |
| 163 | + |
| 164 | +```typescript |
| 165 | +// We need a version of `captureException` from somewhere. Here, we are getting |
| 166 | +it from `@sentry/browser`. |
| 167 | +import { captureException } from '@sentry/browser'; |
| 168 | + |
| 169 | +// We also need to get the ErrorReportingService. |
| 170 | +import { ErrorReportingService } from '@metamask/error-reporting-service'; |
| 171 | + |
| 172 | +// We also bring in our function. |
| 173 | +import { doSomething } from './do-something'; |
| 174 | + |
| 175 | +// We create a new instance of the ErrorReportingService. |
| 176 | +const errorReportingService = new ErrorReportingService({ captureException }); |
| 177 | + |
| 178 | +// Now we call our function, and it will report the error in Sentry. |
| 179 | +doSomething(errorReportingService); |
| 180 | +``` |
| 181 | + |
| 182 | +#### 2A. Calling file (React Native) |
| 183 | + |
| 184 | +```typescript |
| 185 | +// We need a version of `captureException` from somewhere. Here, we are getting |
| 186 | +it from `@sentry/react-native`. |
| 187 | +import { captureException } from '@sentry/react-native'; |
| 188 | + |
| 189 | +// We also need to get the ErrorReportingService. |
| 190 | +import { ErrorReportingService } from '@metamask/error-reporting-service'; |
| 191 | + |
| 192 | +// We also bring in our function. |
| 193 | +import { doSomething } from './do-something'; |
| 194 | + |
| 195 | +// We create a new instance of the ErrorReportingService. |
| 196 | +const errorReportingService = new ErrorReportingService({ captureException }); |
| 197 | + |
| 198 | +// Now we call our function, and it will report the error in Sentry. |
| 199 | +doSomething(errorReportingService); |
| 200 | +``` |
| 201 | + |
| 202 | +## Contributing |
| 203 | + |
| 204 | +This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme). |
0 commit comments