From 0552a5faaca19c7f723527f655d7c93f9e0ec0a3 Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Tue, 29 Oct 2024 19:43:20 +0000 Subject: [PATCH 1/3] Create extremely basic error class --- lib/util/apiError.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lib/util/apiError.js diff --git a/lib/util/apiError.js b/lib/util/apiError.js new file mode 100644 index 000000000..e980cec5f --- /dev/null +++ b/lib/util/apiError.js @@ -0,0 +1,16 @@ +// The data should be a response object + +class RobloxAPIError extends Error { + constructor(data) { + this.httpStatusCode = data.statusCode + this.responseBody = (function () { + if (typeof data.body === "string") return data.body; + + try { + return JSON.stringify(data.body) + } catch { + throw Error("The passed response body is not a valid object") + } + }) + } +} From a654bd9dcb29bf24c8966b9542742798853100df Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Tue, 29 Oct 2024 20:29:54 +0000 Subject: [PATCH 2/3] Make most of class --- lib/util/apiError.js | 45 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/util/apiError.js b/lib/util/apiError.js index e980cec5f..67e7046e4 100644 --- a/lib/util/apiError.js +++ b/lib/util/apiError.js @@ -1,16 +1,41 @@ // The data should be a response object class RobloxAPIError extends Error { - constructor(data) { + constructor (data) { + super(getResponseBody(data)) + + this.name = 'RobloxAPIError' this.httpStatusCode = data.statusCode - this.responseBody = (function () { - if (typeof data.body === "string") return data.body; - - try { - return JSON.stringify(data.body) - } catch { - throw Error("The passed response body is not a valid object") - } - }) + this.responseBody = getResponseBody(data) + } + + get error () { + let obj + + try { + obj = JSON.parse(this.responseBody) + } catch { + return { code: 0, message: this.responseBody.toString() } + } + + if (Object.hasOwn(obj, 'error')) return obj.error // V1 open cloud and some very old BEDEV1 endpoints + some global errors + + else if (Object.hasOwn(obj, 'errors')) { // Most BEDEV1 endpoints + return obj.errors.at(0) // In spite of BEDEV1 endpoint errors being nested in an array, they are never seen in groups. + } else if (Object.hasOwn(obj, 'code') && Object.hasOwn(obj, 'message')) { // V2 open cloid + return obj + } else return { code: 0, message: this.responseBody } // Roblox did a funny (i.e. the platform is down) } } + +function getResponseBody (data) { + if (typeof data.body === 'string') return data.body + + try { + return JSON.stringify(data.body) + } catch { + throw Error('The passed response body is not a valid object') + } +} + +export default RobloxAPIError From 75f391787c7314c41f05791e2f56114f9acfe3db Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:30:52 -0500 Subject: [PATCH 3/3] Fix typo --- lib/util/apiError.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util/apiError.js b/lib/util/apiError.js index 67e7046e4..91796211f 100644 --- a/lib/util/apiError.js +++ b/lib/util/apiError.js @@ -22,7 +22,7 @@ class RobloxAPIError extends Error { else if (Object.hasOwn(obj, 'errors')) { // Most BEDEV1 endpoints return obj.errors.at(0) // In spite of BEDEV1 endpoint errors being nested in an array, they are never seen in groups. - } else if (Object.hasOwn(obj, 'code') && Object.hasOwn(obj, 'message')) { // V2 open cloid + } else if (Object.hasOwn(obj, 'code') && Object.hasOwn(obj, 'message')) { // V2 open cloud return obj } else return { code: 0, message: this.responseBody } // Roblox did a funny (i.e. the platform is down) }