From efb89d8cd3a0ec465985dfaffeec99329704ba8b Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:19:31 -0500 Subject: [PATCH] Add RobloxAPIError class (#841) * Create extremely basic error class * Make most of class * Fix typo --- lib/util/apiError.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 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..91796211f --- /dev/null +++ b/lib/util/apiError.js @@ -0,0 +1,41 @@ +// The data should be a response object + +class RobloxAPIError extends Error { + constructor (data) { + super(getResponseBody(data)) + + this.name = 'RobloxAPIError' + this.httpStatusCode = data.statusCode + 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 cloud + 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