|
2 | 2 | import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types'; |
3 | 3 | import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils'; |
4 | 4 |
|
5 | | -import { spanStatusfromHttpCode, SpanStatusType } from './spanstatus'; |
6 | | - |
7 | 5 | /** |
8 | 6 | * Keeps track of finished spans for a given transaction |
9 | 7 | * @internal |
@@ -340,3 +338,85 @@ export class Span implements SpanInterface { |
340 | 338 | }); |
341 | 339 | } |
342 | 340 | } |
| 341 | + |
| 342 | +export type SpanStatusType = |
| 343 | + /** The operation completed successfully. */ |
| 344 | + | 'ok' |
| 345 | + /** Deadline expired before operation could complete. */ |
| 346 | + | 'deadline_exceeded' |
| 347 | + /** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */ |
| 348 | + | 'unauthenticated' |
| 349 | + /** 403 Forbidden */ |
| 350 | + | 'permission_denied' |
| 351 | + /** 404 Not Found. Some requested entity (file or directory) was not found. */ |
| 352 | + | 'not_found' |
| 353 | + /** 429 Too Many Requests */ |
| 354 | + | 'resource_exhausted' |
| 355 | + /** Client specified an invalid argument. 4xx. */ |
| 356 | + | 'invalid_argument' |
| 357 | + /** 501 Not Implemented */ |
| 358 | + | 'unimplemented' |
| 359 | + /** 503 Service Unavailable */ |
| 360 | + | 'unavailable' |
| 361 | + /** Other/generic 5xx. */ |
| 362 | + | 'internal_error' |
| 363 | + /** Unknown. Any non-standard HTTP status code. */ |
| 364 | + | 'unknown_error' |
| 365 | + /** The operation was cancelled (typically by the user). */ |
| 366 | + | 'cancelled' |
| 367 | + /** Already exists (409) */ |
| 368 | + | 'already_exists' |
| 369 | + /** Operation was rejected because the system is not in a state required for the operation's */ |
| 370 | + | 'failed_precondition' |
| 371 | + /** The operation was aborted, typically due to a concurrency issue. */ |
| 372 | + | 'aborted' |
| 373 | + /** Operation was attempted past the valid range. */ |
| 374 | + | 'out_of_range' |
| 375 | + /** Unrecoverable data loss or corruption */ |
| 376 | + | 'data_loss'; |
| 377 | + |
| 378 | +/** |
| 379 | + * Converts a HTTP status code into a {@link SpanStatusType}. |
| 380 | + * |
| 381 | + * @param httpStatus The HTTP response status code. |
| 382 | + * @returns The span status or unknown_error. |
| 383 | + */ |
| 384 | +export function spanStatusfromHttpCode(httpStatus: number): SpanStatusType { |
| 385 | + if (httpStatus < 400 && httpStatus >= 100) { |
| 386 | + return 'ok'; |
| 387 | + } |
| 388 | + |
| 389 | + if (httpStatus >= 400 && httpStatus < 500) { |
| 390 | + switch (httpStatus) { |
| 391 | + case 401: |
| 392 | + return 'unauthenticated'; |
| 393 | + case 403: |
| 394 | + return 'permission_denied'; |
| 395 | + case 404: |
| 396 | + return 'not_found'; |
| 397 | + case 409: |
| 398 | + return 'already_exists'; |
| 399 | + case 413: |
| 400 | + return 'failed_precondition'; |
| 401 | + case 429: |
| 402 | + return 'resource_exhausted'; |
| 403 | + default: |
| 404 | + return 'invalid_argument'; |
| 405 | + } |
| 406 | + } |
| 407 | + |
| 408 | + if (httpStatus >= 500 && httpStatus < 600) { |
| 409 | + switch (httpStatus) { |
| 410 | + case 501: |
| 411 | + return 'unimplemented'; |
| 412 | + case 503: |
| 413 | + return 'unavailable'; |
| 414 | + case 504: |
| 415 | + return 'deadline_exceeded'; |
| 416 | + default: |
| 417 | + return 'internal_error'; |
| 418 | + } |
| 419 | + } |
| 420 | + |
| 421 | + return 'unknown_error'; |
| 422 | +} |
0 commit comments