1- import type { Span } from '@sentry/types' ;
1+ import type { Span , SpanStatus } from '@sentry/types' ;
22
3- /** The status of an Span.
4- *
5- * @deprecated Use string literals - if you require type casting, cast to SpanStatusType type
6- */
7- export enum SpanStatus {
8- /** The operation completed successfully. */
9- Ok = 'ok' ,
10- /** Deadline expired before operation could complete. */
11- DeadlineExceeded = 'deadline_exceeded' ,
12- /** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */
13- Unauthenticated = 'unauthenticated' ,
14- /** 403 Forbidden */
15- PermissionDenied = 'permission_denied' ,
16- /** 404 Not Found. Some requested entity (file or directory) was not found. */
17- NotFound = 'not_found' ,
18- /** 429 Too Many Requests */
19- ResourceExhausted = 'resource_exhausted' ,
20- /** Client specified an invalid argument. 4xx. */
21- InvalidArgument = 'invalid_argument' ,
22- /** 501 Not Implemented */
23- Unimplemented = 'unimplemented' ,
24- /** 503 Service Unavailable */
25- Unavailable = 'unavailable' ,
26- /** Other/generic 5xx. */
27- InternalError = 'internal_error' ,
28- /** Unknown. Any non-standard HTTP status code. */
29- UnknownError = 'unknown_error' ,
30- /** The operation was cancelled (typically by the user). */
31- Cancelled = 'cancelled' ,
32- /** Already exists (409) */
33- AlreadyExists = 'already_exists' ,
34- /** Operation was rejected because the system is not in a state required for the operation's */
35- FailedPrecondition = 'failed_precondition' ,
36- /** The operation was aborted, typically due to a concurrency issue. */
37- Aborted = 'aborted' ,
38- /** Operation was attempted past the valid range. */
39- OutOfRange = 'out_of_range' ,
40- /** Unrecoverable data loss or corruption */
41- DataLoss = 'data_loss' ,
42- }
43-
44- export type SpanStatusType =
45- /** The operation completed successfully. */
46- | 'ok'
47- /** Deadline expired before operation could complete. */
48- | 'deadline_exceeded'
49- /** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */
50- | 'unauthenticated'
51- /** 403 Forbidden */
52- | 'permission_denied'
53- /** 404 Not Found. Some requested entity (file or directory) was not found. */
54- | 'not_found'
55- /** 429 Too Many Requests */
56- | 'resource_exhausted'
57- /** Client specified an invalid argument. 4xx. */
58- | 'invalid_argument'
59- /** 501 Not Implemented */
60- | 'unimplemented'
61- /** 503 Service Unavailable */
62- | 'unavailable'
63- /** Other/generic 5xx. */
64- | 'internal_error'
65- /** Unknown. Any non-standard HTTP status code. */
66- | 'unknown_error'
67- /** The operation was cancelled (typically by the user). */
68- | 'cancelled'
69- /** Already exists (409) */
70- | 'already_exists'
71- /** Operation was rejected because the system is not in a state required for the operation's */
72- | 'failed_precondition'
73- /** The operation was aborted, typically due to a concurrency issue. */
74- | 'aborted'
75- /** Operation was attempted past the valid range. */
76- | 'out_of_range'
77- /** Unrecoverable data loss or corruption */
78- | 'data_loss' ;
3+ export const SPAN_STATUS_UNSET = 0 ;
4+ export const SPAN_STATUS_OK = 1 ;
5+ export const SPAN_STATUS_ERROR = 2 ;
796
807/**
81- * Converts a HTTP status code into a { @link SpanStatusType} .
8+ * Converts a HTTP status code into a sentry status with a message .
829 *
8310 * @param httpStatus The HTTP response status code.
8411 * @returns The span status or unknown_error.
8512 */
86- export function getSpanStatusFromHttpCode ( httpStatus : number ) : SpanStatusType {
13+ export function getSpanStatusFromHttpCode ( httpStatus : number ) : SpanStatus {
8714 if ( httpStatus < 400 && httpStatus >= 100 ) {
88- return 'ok' ;
15+ return { code : SPAN_STATUS_OK } ;
8916 }
9017
9118 if ( httpStatus >= 400 && httpStatus < 500 ) {
9219 switch ( httpStatus ) {
9320 case 401 :
94- return 'unauthenticated' ;
21+ return { code : SPAN_STATUS_ERROR , message : 'unauthenticated' } ;
9522 case 403 :
96- return 'permission_denied' ;
23+ return { code : SPAN_STATUS_ERROR , message : 'permission_denied' } ;
9724 case 404 :
98- return 'not_found' ;
25+ return { code : SPAN_STATUS_ERROR , message : 'not_found' } ;
9926 case 409 :
100- return 'already_exists' ;
27+ return { code : SPAN_STATUS_ERROR , message : 'already_exists' } ;
10128 case 413 :
102- return 'failed_precondition' ;
29+ return { code : SPAN_STATUS_ERROR , message : 'failed_precondition' } ;
10330 case 429 :
104- return 'resource_exhausted' ;
31+ return { code : SPAN_STATUS_ERROR , message : 'resource_exhausted' } ;
10532 default :
106- return 'invalid_argument' ;
33+ return { code : SPAN_STATUS_ERROR , message : 'invalid_argument' } ;
10734 }
10835 }
10936
11037 if ( httpStatus >= 500 && httpStatus < 600 ) {
11138 switch ( httpStatus ) {
11239 case 501 :
113- return 'unimplemented' ;
40+ return { code : SPAN_STATUS_ERROR , message : 'unimplemented' } ;
11441 case 503 :
115- return 'unavailable' ;
42+ return { code : SPAN_STATUS_ERROR , message : 'unavailable' } ;
11643 case 504 :
117- return 'deadline_exceeded' ;
44+ return { code : SPAN_STATUS_ERROR , message : 'deadline_exceeded' } ;
11845 default :
119- return 'internal_error' ;
46+ return { code : SPAN_STATUS_ERROR , message : 'internal_error' } ;
12047 }
12148 }
12249
123- return 'unknown_error' ;
50+ return { code : SPAN_STATUS_ERROR , message : 'unknown_error' } ;
12451}
12552
12653/**
@@ -131,7 +58,7 @@ export function setHttpStatus(span: Span, httpStatus: number): void {
13158 span . setAttribute ( 'http.response.status_code' , httpStatus ) ;
13259
13360 const spanStatus = getSpanStatusFromHttpCode ( httpStatus ) ;
134- if ( spanStatus !== 'unknown_error' ) {
61+ if ( spanStatus . message !== 'unknown_error' ) {
13562 span . setStatus ( spanStatus ) ;
13663 }
13764}
0 commit comments