@sanity/client 7.21.0 → 7.22.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.
@@ -99,6 +99,7 @@ class ClientError extends Error {
99
99
  response;
100
100
  statusCode = 400;
101
101
  responseBody;
102
+ traceId;
102
103
  details;
103
104
  constructor(res, context) {
104
105
  const props = extractErrorProps(res, context);
@@ -109,6 +110,7 @@ class ServerError extends Error {
109
110
  response;
110
111
  statusCode = 500;
111
112
  responseBody;
113
+ traceId;
112
114
  details;
113
115
  constructor(res) {
114
116
  const props = extractErrorProps(res);
@@ -120,29 +122,30 @@ function extractErrorProps(res, context) {
120
122
  response: res,
121
123
  statusCode: res.statusCode,
122
124
  responseBody: stringifyBody(body, res),
125
+ traceId: extractTraceId(res),
123
126
  message: "",
124
127
  details: void 0
125
128
  };
126
129
  if (!stegaClean.isRecord(body))
127
- return props.message = httpErrorMessage(res, body), props;
130
+ return props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
128
131
  const error = body.error;
129
132
  if (typeof error == "string" && typeof body.message == "string")
130
- return props.message = `${error} - ${body.message}`, props;
133
+ return props.message = `${error} - ${body.message}${formatTraceId(props.traceId)}`, props;
131
134
  if (typeof error != "object" || error === null)
132
- return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
135
+ return typeof error == "string" ? props.message = `${error}${formatTraceId(props.traceId)}` : typeof body.message == "string" ? props.message = `${body.message}${formatTraceId(props.traceId)}` : props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
133
136
  if (isMutationError(error) || isActionError(error)) {
134
137
  const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
135
138
  let itemsStr = items.length ? `:
136
139
  - ${items.join(`
137
140
  - `)}` : "";
138
141
  return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
139
- ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
142
+ ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${formatTraceId(props.traceId)}${itemsStr}`, props.details = body.error, props;
140
143
  }
141
144
  if (isQueryParseError(error)) {
142
145
  const tag = context?.options?.query?.tag;
143
- return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
146
+ return props.message = formatQueryParseError(error, tag, props.traceId), props.details = body.error, props;
144
147
  }
145
- return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
148
+ return "description" in error && typeof error.description == "string" ? (props.message = `${error.description}${formatTraceId(props.traceId)}`, props.details = error, props) : (props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props);
146
149
  }
147
150
  function isMutationError(error) {
148
151
  return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
@@ -153,23 +156,32 @@ function isActionError(error) {
153
156
  function isQueryParseError(error) {
154
157
  return stegaClean.isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
155
158
  }
156
- function formatQueryParseError(error, tag) {
157
- const { query, start, end, description } = error;
159
+ function formatQueryParseError(error, tag, traceId) {
160
+ const { query, start, end, description } = error, withTraceId = traceId ? `
161
+ (traceId: ${traceId})` : "";
158
162
  if (!query || typeof start > "u")
159
- return `GROQ query parse error: ${description}`;
163
+ return `GROQ query parse error: ${description}${withTraceId}`;
160
164
  const withTag = tag ? `
161
165
 
162
166
  Tag: ${tag}` : "";
163
167
  return `GROQ query parse error:
164
- ${codeFrame(query, { start, end }, description)}${withTag}`;
168
+ ${codeFrame(query, { start, end }, description)}${withTag}${withTraceId}`;
165
169
  }
166
170
  function httpErrorMessage(res, body) {
167
171
  const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
168
172
  return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
169
173
  }
174
+ function extractTraceId(res) {
175
+ const traceparent = res?.headers?.traceparent;
176
+ if (traceparent)
177
+ return traceparent.split("-")[1];
178
+ }
170
179
  function stringifyBody(body, res) {
171
180
  return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
172
181
  }
182
+ function formatTraceId(traceId) {
183
+ return traceId ? ` (traceId: ${traceId})` : "";
184
+ }
173
185
  function sliceWithEllipsis(str, max) {
174
186
  return str.length > max ? `${str.slice(0, max)}\u2026` : str;
175
187
  }