Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Expose statusCode in OCSError #1245

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog/unreleased/enhancement-add-ocs-error
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ We've added the new OCSError class,
this allows developers to further process the error information.

https://github.com/owncloud/owncloud-sdk/pull/1237
https://github.com/owncloud/owncloud-sdk/pull/1245
4 changes: 3 additions & 1 deletion src/OCSError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
* @class OCSError
* @classdesc A error class for OCS errors
* @param {string} message message to be added to the OCSError
* @param {number} statusCode statusCode to be added to the OCSError
* @param {object} response http response
* @param {object} ocs ocs additional data

*/

class OCSError extends Error {
constructor (message, response, ocs = null) {
constructor (message, statusCode, response, ocs = null) {
super(message)
this.statusCode = statusCode
this.response = response
this.ocs = ocs
}
Expand Down
11 changes: 6 additions & 5 deletions src/helperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class helpers {
})
.then(res => {
if (res.status >= 400) {
throw new OCSError(res.statusText, res)
throw new OCSError(res.statusText, res.status, res)
}
return res
})
Expand All @@ -277,17 +277,18 @@ class helpers {
try {
tree = JSON.parse(body)
} catch (e) {
throw new OCSError(e.message, res)
throw new OCSError(e.message, 422, res)
}
}
if ('message' in tree) {
throw new OCSError(tree.message, res)
throw new OCSError(tree.message, tree.statuscode ? parseInt(tree.statuscode) : null, res)
}
const error = self._checkOCSstatus(tree)
if (error) {
const errorMessage = typeof error === 'object' ? error.ocs.meta.message : error
const ocsData = typeof error === 'object' ? error : {}
throw new OCSError(errorMessage, res, ocsData.ocs)
const ocsData = typeof error === 'object' ? error.ocs : {}
const statusCode = tree?.ocs?.meta?.statuscode ? parseInt(tree.ocs.meta.statuscode) : null
throw new OCSError(errorMessage, statusCode, res, ocsData)
}
res.statusCode = res.status
resolve({
Expand Down