From ed9b4b278649faef3a2a92155e124f5e06641f2e Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Fri, 15 Dec 2023 08:11:40 -0800 Subject: [PATCH] throw with error messages or SERVER_ERROR if network response is not ok --- package.json | 2 +- src/api/api.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3052eae27..49b937a57 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opensea-js", - "version": "7.0.1", + "version": "7.0.2", "description": "TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data", "license": "MIT", "author": "OpenSea Developers", diff --git a/src/api/api.ts b/src/api/api.ts index e13483926..a37c054f8 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -620,6 +620,17 @@ export class OpenSeaAPI { ); const response = await req.send(); + if (!response.ok) { + // If an errors array is returned, throw with the error messages. + const errors = response.bodyJson?.errors; + if (errors?.length > 0) { + throw new Error(`Server Error: ${errors.join(", ")}`); + } else { + // Otherwise, let ethers throw a SERVER_ERROR error since it will include + // more context about the request and response. + response.assertOk(); + } + } return response.bodyJson; } }