Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RobloxAPIError class #841

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/util/apiError.js
Original file line number Diff line number Diff line change
@@ -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
Loading