|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | import { compose, toHashMap } from "@appsignal/core"
|
7 |
| -import type { Breadcrumb, JSClient, Hook } from "@appsignal/types" |
| 7 | +import type { Breadcrumb, JSClient, Hook, HashMap } from "@appsignal/types" |
8 | 8 |
|
9 | 9 | import { VERSION } from "./version"
|
10 | 10 | import { PushApi } from "./api"
|
@@ -70,6 +70,16 @@ export default class Appsignal implements JSClient {
|
70 | 70 | this._options = options
|
71 | 71 | }
|
72 | 72 |
|
| 73 | + /** |
| 74 | + * Records and sends a browser `Error` to AppSignal. |
| 75 | + * |
| 76 | + * @param {Error} error A JavaScript Error object |
| 77 | + * @param {Function | void} fn Optional callback function to modify span before it's sent. |
| 78 | + * |
| 79 | + * @return {Promise<Span> | void} An API response, or `void` if `Promise` is unsupported. |
| 80 | + */ |
| 81 | + public send<T>(error: Error, fn?: (span: Span) => T): Promise<Span> | void |
| 82 | + |
73 | 83 | /**
|
74 | 84 | * Records and sends a browser `Error` to AppSignal.
|
75 | 85 | *
|
@@ -97,14 +107,14 @@ export default class Appsignal implements JSClient {
|
97 | 107 | /**
|
98 | 108 | *
|
99 | 109 | * @param {Error | Span} data A JavaScript Error or Appsignal Span object
|
100 |
| - * @param {object} tags An key, value object of tags |
101 |
| - * @param {string} namespace An optional namespace name |
| 110 | + * @param {object | Function} tagsOrFn An key-value object of tags or a callback function to customize the span before it is sent. |
| 111 | + * @param {string} namespace DEPRECATED: An optional namespace name. |
102 | 112 | *
|
103 | 113 | * @return {Promise<Span> | void} An API response, or `void` if `Promise` is unsupported.
|
104 | 114 | */
|
105 |
| - public send( |
| 115 | + public send<T>( |
106 | 116 | data: Error | Span,
|
107 |
| - tags = {}, |
| 117 | + tagsOrFn?: object | ((span: Span) => T), |
108 | 118 | namespace?: string
|
109 | 119 | ): Promise<any> | void {
|
110 | 120 | if (!(data instanceof Error) && !(data instanceof Span)) {
|
@@ -148,8 +158,25 @@ export default class Appsignal implements JSClient {
|
148 | 158 | compose(...this._hooks.decorators)(span)
|
149 | 159 | }
|
150 | 160 |
|
151 |
| - if (tags) span.setTags(tags) |
152 |
| - if (namespace) span.setNamespace(namespace) |
| 161 | + if (tagsOrFn) { |
| 162 | + if (typeof tagsOrFn === "function") { |
| 163 | + const callback = tagsOrFn |
| 164 | + callback(span) |
| 165 | + } else { |
| 166 | + console.warn( |
| 167 | + "[APPSIGNAL]: DEPRECATED: Calling the `send`/`sendError` function with a tags object is deprecated. Use the callback argument instead." |
| 168 | + ) |
| 169 | + const tags = (toHashMap(tagsOrFn) || {}) as HashMap<string> |
| 170 | + span.setTags(tags) |
| 171 | + } |
| 172 | + } |
| 173 | + if (namespace) { |
| 174 | + console.warn( |
| 175 | + "[APPSIGNAL]: DEPRECATED: Calling the `send`/`sendError` function with a namespace is deprecated. Use the callback argument instead." |
| 176 | + ) |
| 177 | + span.setNamespace(namespace) |
| 178 | + } |
| 179 | + |
153 | 180 | if (this._breadcrumbs.length > 0) span.setBreadcrumbs(this._breadcrumbs)
|
154 | 181 |
|
155 | 182 | // A Span can be "overridden" with metadata after it has been created,
|
@@ -194,22 +221,30 @@ export default class Appsignal implements JSClient {
|
194 | 221 | }
|
195 | 222 | }
|
196 | 223 |
|
| 224 | + sendError<T>(error: Error): Promise<Span> | void |
| 225 | + sendError<T>(error: Error, callback: (span: Span) => T): Promise<Span> | void |
| 226 | + sendError<T>( |
| 227 | + error: Error, |
| 228 | + tags?: object, |
| 229 | + namespace?: string |
| 230 | + ): Promise<Span> | void |
| 231 | + |
197 | 232 | /**
|
198 | 233 | * Records and sends a browser `Error` to AppSignal. An alias to `#send()`
|
199 | 234 | * to maintain compatibility.
|
200 | 235 | *
|
201 | 236 | * @param {Error} error A JavaScript Error object
|
202 |
| - * @param {object} tags An key, value object of tags |
203 |
| - * @param {string} namespace An optional namespace name |
| 237 | + * @param {object | Function} tagsOrFn An key-value object of tags or callback function to customize the span before it is sent. |
| 238 | + * @param {string} namespace DEPRECATED: An optional namespace name |
204 | 239 | *
|
205 | 240 | * @return {Promise<Span> | void} An API response, or `void` if `Promise` is unsupported.
|
206 | 241 | */
|
207 |
| - public sendError( |
| 242 | + public sendError<T>( |
208 | 243 | error: Error,
|
209 |
| - tags?: object, |
| 244 | + tagsOrFn?: object | ((span: Span) => T), |
210 | 245 | namespace?: string
|
211 | 246 | ): Promise<Span> | void {
|
212 |
| - return this.send(error, tags, namespace) |
| 247 | + return this.send(error, tagsOrFn, namespace) |
213 | 248 | }
|
214 | 249 |
|
215 | 250 | /**
|
@@ -250,22 +285,44 @@ export default class Appsignal implements JSClient {
|
250 | 285 | return span
|
251 | 286 | }
|
252 | 287 |
|
| 288 | + /** |
| 289 | + * Wraps and catches errors within a given function. If the function throws an |
| 290 | + * error, a rejected `Promise` will be returned and the error thrown will be |
| 291 | + * logged to AppSignal. |
| 292 | + */ |
| 293 | + public async wrap<T>(fn: () => T, callbackFn?: (span: Span) => T): Promise<T> |
| 294 | + |
| 295 | + /** |
| 296 | + * Wraps and catches errors within a given function. If the function throws an |
| 297 | + * error, a rejected `Promise` will be returned and the error thrown will be |
| 298 | + * logged to AppSignal. |
| 299 | + */ |
| 300 | + public async wrap<T>( |
| 301 | + fn: () => T, |
| 302 | + tags?: object, |
| 303 | + namespace?: string |
| 304 | + ): Promise<T> |
| 305 | + |
253 | 306 | /**
|
254 | 307 | * Wraps and catches errors within a given function. If the function throws an
|
255 | 308 | * error, a rejected `Promise` will be returned and the error thrown will be
|
256 | 309 | * logged to AppSignal.
|
257 | 310 | *
|
258 | 311 | * @param {Function} fn A function to wrap
|
259 |
| - * @param {object} tags An key, value object of tags |
260 |
| - * @param {string} namespace An optional namespace name |
| 312 | + * @param {object | Function} tagsOrFn An key-value object of tags or a callback function to customize the span before it is sent. |
| 313 | + * @param {string} namespace DEPRECATED: An optional namespace name |
261 | 314 | *
|
262 | 315 | * @return {Promise<any>} A Promise containing the return value of the function, or a `Span` if an error was thrown.
|
263 | 316 | */
|
264 |
| - public async wrap<T>(fn: () => T, tags = {}, namespace?: string): Promise<T> { |
| 317 | + public async wrap<T>( |
| 318 | + fn: () => T, |
| 319 | + tagsOrFn?: object | ((span: Span) => T), |
| 320 | + namespace?: string |
| 321 | + ): Promise<T> { |
265 | 322 | try {
|
266 | 323 | return await fn()
|
267 | 324 | } catch (e) {
|
268 |
| - await this.sendError(e, tags, namespace) |
| 325 | + await this.sendError(e, tagsOrFn, namespace) |
269 | 326 | return Promise.reject(e)
|
270 | 327 | }
|
271 | 328 | }
|
|
0 commit comments