@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
86 lines (85 loc) • 2.21 kB
JavaScript
import { SPAN_STATUS_TYPES } from "../types/spanStatus.js";
import { HTTP_RESPONSE_STATUS_CODE } from "@sentry/conventions/attributes";
// src/tracing/spanstatus.ts
const SPAN_STATUS_UNSET = 0;
const SPAN_STATUS_OK = 1;
const SPAN_STATUS_ERROR = 2;
function isStatusErrorMessageValid(message) {
return message !== "ok" && SPAN_STATUS_TYPES.includes(message);
}
/**
* Converts a HTTP status code into a sentry status with a message.
*
* @param httpStatus The HTTP response status code.
* @returns The span status or internal_error.
*/
function getSpanStatusFromHttpCode(httpStatus) {
if (httpStatus < 400 && httpStatus >= 100) return { code: 1 };
if (httpStatus >= 400 && httpStatus < 500) switch (httpStatus) {
case 401: return {
code: 2,
message: "unauthenticated"
};
case 403: return {
code: 2,
message: "permission_denied"
};
case 404: return {
code: 2,
message: "not_found"
};
case 409: return {
code: 2,
message: "already_exists"
};
case 413: return {
code: 2,
message: "failed_precondition"
};
case 429: return {
code: 2,
message: "resource_exhausted"
};
case 499: return {
code: 2,
message: "cancelled"
};
default: return {
code: 2,
message: "invalid_argument"
};
}
if (httpStatus >= 500 && httpStatus < 600) switch (httpStatus) {
case 501: return {
code: 2,
message: "unimplemented"
};
case 503: return {
code: 2,
message: "unavailable"
};
case 504: return {
code: 2,
message: "deadline_exceeded"
};
default: return {
code: 2,
message: "internal_error"
};
}
return {
code: 2,
message: "internal_error"
};
}
/**
* Sets the Http status attributes on the current span based on the http code.
* Additionally, the span's status is updated, depending on the http code.
*/
function setHttpStatus(span, httpStatus) {
span.setAttribute(HTTP_RESPONSE_STATUS_CODE, httpStatus);
const spanStatus = getSpanStatusFromHttpCode(httpStatus);
if (spanStatus.message !== "unknown_error") span.setStatus(spanStatus);
}
export { SPAN_STATUS_ERROR, SPAN_STATUS_OK, SPAN_STATUS_UNSET, getSpanStatusFromHttpCode, isStatusErrorMessageValid, setHttpStatus };
//# sourceMappingURL=spanstatus.js.map