error.cjs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.LangSmithConflictError = void 0;
  4. exports.printErrorStackTrace = printErrorStackTrace;
  5. exports.raiseForStatus = raiseForStatus;
  6. function getErrorStackTrace(e) {
  7. if (typeof e !== "object" || e == null)
  8. return undefined;
  9. if (!("stack" in e) || typeof e.stack !== "string")
  10. return undefined;
  11. let stack = e.stack;
  12. const prevLine = `${e}`;
  13. if (stack.startsWith(prevLine)) {
  14. stack = stack.slice(prevLine.length);
  15. }
  16. if (stack.startsWith("\n")) {
  17. stack = stack.slice(1);
  18. }
  19. return stack;
  20. }
  21. function printErrorStackTrace(e) {
  22. const stack = getErrorStackTrace(e);
  23. if (stack == null)
  24. return;
  25. console.error(stack);
  26. }
  27. /**
  28. * LangSmithConflictError
  29. *
  30. * Represents an error that occurs when there's a conflict during an operation,
  31. * typically corresponding to HTTP 409 status code responses.
  32. *
  33. * This error is thrown when an attempt to create or modify a resource conflicts
  34. * with the current state of the resource on the server. Common scenarios include:
  35. * - Attempting to create a resource that already exists
  36. * - Trying to update a resource that has been modified by another process
  37. * - Violating a uniqueness constraint in the data
  38. *
  39. * @extends Error
  40. *
  41. * @example
  42. * try {
  43. * await createProject("existingProject");
  44. * } catch (error) {
  45. * if (error instanceof ConflictError) {
  46. * console.log("A conflict occurred:", error.message);
  47. * // Handle the conflict, e.g., by suggesting a different project name
  48. * } else {
  49. * // Handle other types of errors
  50. * }
  51. * }
  52. *
  53. * @property {string} name - Always set to 'ConflictError' for easy identification
  54. * @property {string} message - Detailed error message including server response
  55. *
  56. * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
  57. */
  58. class LangSmithConflictError extends Error {
  59. constructor(message) {
  60. super(message);
  61. Object.defineProperty(this, "status", {
  62. enumerable: true,
  63. configurable: true,
  64. writable: true,
  65. value: void 0
  66. });
  67. this.name = "LangSmithConflictError";
  68. this.status = 409;
  69. }
  70. }
  71. exports.LangSmithConflictError = LangSmithConflictError;
  72. /**
  73. * Throws an appropriate error based on the response status and body.
  74. *
  75. * @param response - The fetch Response object
  76. * @param context - Additional context to include in the error message (e.g., operation being performed)
  77. * @throws {LangSmithConflictError} When the response status is 409
  78. * @throws {Error} For all other non-ok responses
  79. */
  80. async function raiseForStatus(response, context, consume) {
  81. // consume the response body to release the connection
  82. // https://undici.nodejs.org/#/?id=garbage-collection
  83. let errorBody;
  84. if (response.ok) {
  85. if (consume) {
  86. errorBody = await response.text();
  87. }
  88. return;
  89. }
  90. errorBody = await response.text();
  91. const fullMessage = `Failed to ${context}. Received status [${response.status}]: ${response.statusText}. Server response: ${errorBody}`;
  92. if (response.status === 409) {
  93. throw new LangSmithConflictError(fullMessage);
  94. }
  95. const err = new Error(fullMessage);
  96. err.status = response.status;
  97. throw err;
  98. }