All files / src/utils httpStatus.ts

100% Statements 37/37
100% Branches 23/23
100% Functions 2/2
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78  6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x   6x     6x 19x 4x     15x 10x   1x   1x   1x   1x   1x   3x   2x       5x 4x   1x   1x   1x   1x       1x    
/** The status of an Span. */
export enum SpanStatus {
  /** The operation completed successfully. */
  Ok = 'ok',
  /** Deadline expired before operation could complete. */
  DeadlineExceeded = 'deadline_exceeded',
  /** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */
  Unauthenticated = 'unauthenticated',
  /** 403 Forbidden */
  PermissionDenied = 'permission_denied',
  /** 404 Not Found. Some requested entity (file or directory) was not found. */
  NotFound = 'not_found',
  /** 429 Too Many Requests */
  ResourceExhausted = 'resource_exhausted',
  /** Client specified an invalid argument. 4xx. */
  InvalidArgument = 'invalid_argument',
  /** 501 Not Implemented */
  Unimplemented = 'unimplemented',
  /** 503 Service Unavailable */
  Unavailable = 'unavailable',
  /** Other/generic 5xx. */
  InternalError = 'internal_error',
  /** Unknown. Any non-standard HTTP status code. */
  UnknownError = 'unknown_error',
  /** The operation was cancelled (typically by the user). */
  Cancelled = 'cancelled',
  /** Already exists (409) */
  AlreadyExists = 'already_exists',
  /** Operation was rejected because the system is not in a state required for the operation's */
  FailedPrecondition = 'failed_precondition',
  /** The operation was aborted, typically due to a concurrency issue. */
  Aborted = 'aborted',
  /** Operation was attempted past the valid range. */
  OutOfRange = 'out_of_range',
  /** Unrecoverable data loss or corruption */
  DataLoss = 'data_loss'
}
 
export function fromHttpStatus(httpStatus: number): SpanStatus {
  if (httpStatus < 400) {
    return SpanStatus.Ok
  }
 
  if (httpStatus >= 400 && httpStatus < 500) {
    switch (httpStatus) {
      case 401:
        return SpanStatus.Unauthenticated
      case 403:
        return SpanStatus.PermissionDenied
      case 404:
        return SpanStatus.NotFound
      case 409:
        return SpanStatus.AlreadyExists
      case 413:
        return SpanStatus.FailedPrecondition
      case 429:
        return SpanStatus.ResourceExhausted
      default:
        return SpanStatus.InvalidArgument
    }
  }
 
  if (httpStatus >= 500 && httpStatus < 600) {
    switch (httpStatus) {
      case 501:
        return SpanStatus.Unimplemented
      case 503:
        return SpanStatus.Unavailable
      case 504:
        return SpanStatus.DeadlineExceeded
      default:
        return SpanStatus.InternalError
    }
  }
 
  return SpanStatus.UnknownError
}