@argos-ci/api-client 0.22.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-D7D4PA-g.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import createFetchClient from "openapi-fetch";
3
3
  import createDebug from "debug";
4
4
  import pRetry from "p-retry";
@@ -7,8 +7,20 @@ const debug = createDebug("@argos-ci/api-client");
7
7
  //#region src/fetch.ts
8
8
  const DEFAULT_TIMEOUT = 3e4;
9
9
  var APIError = class extends Error {
10
- constructor(message) {
11
- super(message);
10
+ /**
11
+ * HTTP status code of the response that triggered the error, when available.
12
+ */
13
+ status;
14
+ /**
15
+ * Raw error payload returned by the API (or the infrastructure in front of
16
+ * it). Useful when the body does not match the expected error shape.
17
+ */
18
+ data;
19
+ constructor(message, options) {
20
+ super(message, options?.cause != null ? { cause: options.cause } : void 0);
21
+ this.name = "APIError";
22
+ this.status = options?.status;
23
+ this.data = options?.data;
12
24
  }
13
25
  };
14
26
  async function createRequestFactory(request, timeout) {
@@ -70,18 +82,70 @@ function createClient(options) {
70
82
  });
71
83
  }
72
84
  /**
73
- * Handle API errors.
85
+ * Maximum length of a raw error body included in the formatted message.
86
+ * Keeps things readable when the body is an HTML error page or a stack trace.
87
+ */
88
+ const MAX_RAW_BODY_LENGTH = 500;
89
+ /**
90
+ * Check whether the value matches the expected API error shape:
91
+ * `{ error: string, details?: [{ message }] }`.
92
+ */
93
+ function isStructuredAPIError(error) {
94
+ return typeof error === "object" && error !== null && "error" in error && typeof error.error === "string" && error.error.length > 0;
95
+ }
96
+ /**
97
+ * Format the HTTP status of a response, e.g. `HTTP 413 Payload Too Large`.
98
+ */
99
+ function formatStatus(response) {
100
+ if (!response) return null;
101
+ return response.statusText ? `HTTP ${response.status} ${response.statusText}` : `HTTP ${response.status}`;
102
+ }
103
+ /**
104
+ * Format a raw (non-structured) error body so it can be shown to the user.
105
+ * Returns `null` when there is nothing meaningful to display.
106
+ */
107
+ function formatRawBody(error) {
108
+ if (error == null) return null;
109
+ let text;
110
+ if (typeof error === "string") text = error;
111
+ else try {
112
+ text = JSON.stringify(error);
113
+ } catch {
114
+ text = String(error);
115
+ }
116
+ const trimmed = text.trim();
117
+ if (!trimmed || trimmed === "{}") return null;
118
+ return trimmed.length > MAX_RAW_BODY_LENGTH ? `${trimmed.slice(0, MAX_RAW_BODY_LENGTH)}…` : trimmed;
119
+ }
120
+ /**
121
+ * Format an API error into a human-readable message.
122
+ *
123
+ * When the body matches the expected `{ error, details }` shape, it is used
124
+ * directly. Otherwise — empty body, HTML error page, plain text, etc., which
125
+ * happens when the response comes from infrastructure in front of the API
126
+ * (proxy, load balancer, CDN, rate limiter) rather than from the API itself —
127
+ * we fall back to the HTTP status and the raw body so the failure stays
128
+ * debuggable instead of surfacing an empty message.
74
129
  */
75
- function formatAPIError(error) {
76
- debug("API error", error);
77
- const detailMessage = error.details?.map((detail) => detail.message).join(", ");
78
- return detailMessage ? `${error.error}: ${detailMessage}` : error.error;
130
+ function formatAPIError(error, response) {
131
+ debug("API error", {
132
+ error,
133
+ status: response?.status
134
+ });
135
+ if (isStructuredAPIError(error)) {
136
+ const detailMessage = error.details?.map((detail) => detail.message).join(", ");
137
+ return detailMessage ? `${error.error}: ${detailMessage}` : error.error;
138
+ }
139
+ return [formatStatus(response), formatRawBody(error)].filter(Boolean).join(": ") || "Unknown API error";
79
140
  }
80
141
  /**
81
142
  * Handle API errors.
82
143
  */
83
- function throwAPIError(error) {
84
- throw new APIError(formatAPIError(error));
144
+ function throwAPIError(error, response) {
145
+ throw new APIError(formatAPIError(error, response), {
146
+ status: response?.status,
147
+ data: error
148
+ });
85
149
  }
86
150
  //#endregion
87
151
  export { APIError, schema_exports as ArgosAPISchema, createClient, formatAPIError, throwAPIError };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@argos-ci/api-client",
3
3
  "description": "Argos API Client library.",
4
- "version": "0.22.0",
4
+ "version": "0.23.0",
5
5
  "author": "Smooth Code",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -54,5 +54,5 @@
54
54
  "openapi-fetch": "^0.17.0",
55
55
  "p-retry": "^8.0.0"
56
56
  },
57
- "gitHead": "4d890d5091bd313c327eb4e8c17dc1bee9d48578"
57
+ "gitHead": "a2fb6e1a811e66f3839293ea5a71e93fee838aac"
58
58
  }