1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.LangSmithConflictError = void 0;
- exports.printErrorStackTrace = printErrorStackTrace;
- exports.raiseForStatus = raiseForStatus;
- function getErrorStackTrace(e) {
- if (typeof e !== "object" || e == null)
- return undefined;
- if (!("stack" in e) || typeof e.stack !== "string")
- return undefined;
- let stack = e.stack;
- const prevLine = `${e}`;
- if (stack.startsWith(prevLine)) {
- stack = stack.slice(prevLine.length);
- }
- if (stack.startsWith("\n")) {
- stack = stack.slice(1);
- }
- return stack;
- }
- function printErrorStackTrace(e) {
- const stack = getErrorStackTrace(e);
- if (stack == null)
- return;
- console.error(stack);
- }
- /**
- * LangSmithConflictError
- *
- * Represents an error that occurs when there's a conflict during an operation,
- * typically corresponding to HTTP 409 status code responses.
- *
- * This error is thrown when an attempt to create or modify a resource conflicts
- * with the current state of the resource on the server. Common scenarios include:
- * - Attempting to create a resource that already exists
- * - Trying to update a resource that has been modified by another process
- * - Violating a uniqueness constraint in the data
- *
- * @extends Error
- *
- * @example
- * try {
- * await createProject("existingProject");
- * } catch (error) {
- * if (error instanceof ConflictError) {
- * console.log("A conflict occurred:", error.message);
- * // Handle the conflict, e.g., by suggesting a different project name
- * } else {
- * // Handle other types of errors
- * }
- * }
- *
- * @property {string} name - Always set to 'ConflictError' for easy identification
- * @property {string} message - Detailed error message including server response
- *
- * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
- */
- class LangSmithConflictError extends Error {
- constructor(message) {
- super(message);
- Object.defineProperty(this, "status", {
- enumerable: true,
- configurable: true,
- writable: true,
- value: void 0
- });
- this.name = "LangSmithConflictError";
- this.status = 409;
- }
- }
- exports.LangSmithConflictError = LangSmithConflictError;
- /**
- * Throws an appropriate error based on the response status and body.
- *
- * @param response - The fetch Response object
- * @param context - Additional context to include in the error message (e.g., operation being performed)
- * @throws {LangSmithConflictError} When the response status is 409
- * @throws {Error} For all other non-ok responses
- */
- async function raiseForStatus(response, context, consume) {
- // consume the response body to release the connection
- // https://undici.nodejs.org/#/?id=garbage-collection
- let errorBody;
- if (response.ok) {
- if (consume) {
- errorBody = await response.text();
- }
- return;
- }
- errorBody = await response.text();
- const fullMessage = `Failed to ${context}. Received status [${response.status}]: ${response.statusText}. Server response: ${errorBody}`;
- if (response.status === 409) {
- throw new LangSmithConflictError(fullMessage);
- }
- const err = new Error(fullMessage);
- err.status = response.status;
- throw err;
- }
|