|
|
@@ -9,6 +9,8 @@ import { Snapshot } from "@/snapshot"
|
|
|
import { fn } from "@/util/fn"
|
|
|
import { Storage } from "@/storage/storage"
|
|
|
import { ProviderTransform } from "@/provider/transform"
|
|
|
+import { STATUS_CODES } from "http"
|
|
|
+import { iife } from "@/util/iife"
|
|
|
|
|
|
export namespace MessageV2 {
|
|
|
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
|
|
@@ -739,7 +741,28 @@ export namespace MessageV2 {
|
|
|
{ cause: e },
|
|
|
).toObject()
|
|
|
case APICallError.isInstance(e):
|
|
|
- const message = ProviderTransform.error(ctx.providerID, e)
|
|
|
+ const message = iife(() => {
|
|
|
+ let msg = e.message
|
|
|
+ const transformed = ProviderTransform.error(ctx.providerID, e)
|
|
|
+ if (transformed !== msg) {
|
|
|
+ return transformed
|
|
|
+ }
|
|
|
+ if (!e.responseBody || (e.statusCode && msg !== STATUS_CODES[e.statusCode])) {
|
|
|
+ return msg
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const body = JSON.parse(e.responseBody)
|
|
|
+ // try to extract common error message fields
|
|
|
+ const errMsg = body.message || body.error
|
|
|
+ if (errMsg && typeof errMsg === "string") {
|
|
|
+ return `${msg}: ${errMsg}`
|
|
|
+ }
|
|
|
+ } catch {}
|
|
|
+
|
|
|
+ return `${msg}: ${e.responseBody}`
|
|
|
+ })
|
|
|
+
|
|
|
return new MessageV2.APIError(
|
|
|
{
|
|
|
message,
|