1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Add MatrixError class. More jsdoc voodoo to get things looking right.

This commit is contained in:
Kegan Dougal
2015-06-05 11:28:28 +01:00
parent 5681517a30
commit fce4d5d1d6
3 changed files with 33 additions and 10 deletions

View File

@@ -189,10 +189,27 @@ var requestCallback = function(userDefinedCallback) {
return userDefinedCallback(err);
}
if (response.statusCode >= 400) {
return userDefinedCallback(body);
return userDefinedCallback(new Error(body));
}
else {
userDefinedCallback(null, body);
}
};
};
/**
* Construct a Matrix error. This is a JavaScript Error with additional
* information specific to the standard Matrix error response.
* @constructor
* @param {Object} errorJson The Matrix error JSON returned from the homeserver.
* @prop {string} name The Matrix 'errcode' value, e.g. "M_FORBIDDEN".
* @prop {string} message The Matrix 'error' value, e.g. "Missing token."
* @prop {Object} data The raw Matrix error JSON used to construct this object.
*/
module.exports.MatrixError = function MatrixError(errorJson) {
this.name = errorJson.errcode || "Unknown error code";
this.message = errorJson.error || "Unknown message";
this.data = errorJson;
}
module.exports.MatrixError.prototype = Object.create(Error.prototype);
module.exports.MatrixError.prototype.constructor = MatrixError;