esrol-errors
Version:
An Errors Class for esrol-server-app. It is used for registering new custom errors with numbers, can get a single or all errors in the registry, has a custom throw error method.
139 lines (113 loc) • 3.82 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: register.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: register.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
/**
* @author Ivaylo Ivanov
* @public
* @class Errors
* @description A Class which holds the registry of errors.
* It also contains methods for setting and getting said
* errors from the registry.
*/
module.exports = class Register {
/**
* @public
* @method constructor
* @description Initialize Register class
*/
constructor() {
this._initializeProperties();
}
/**
* @public
* @method setErrorWithNumber
* @description Add an error message and corresponding number
* to the registry.
* @param {string} description - description text of the error
* @param {int} number - the number that the error will have
* @throws {error} - throws error when incorrect arguments
* have been passed
* @returns {true} - returns true if adding was successful
*/
setErrorWithNumber(description, number) {
if (typeof description !== 'string') {
let e = `setErrorWithNumber expects first param to be description
{string}, ${typeof description} given`;
throw new Error(e);
}
if (!Number.isInteger(number)) {
let e = `setErrorWithNumber expects second param to be integer,
${typeof number} given`;
throw new Error(e);
}
if (!this.registry[number]) {
this.registry[number] = description;
return true;
}
description = this.registry[number];
let e = `Error number ${number} exists with description: ${description}`;
throw new Error(e);
}
/**
* @public
* @method getByNumber
* @description Get an error by it's number.
* @param {int} number - the number that the error has
* @returns {string} - returns the message of the error
*/
getByNumber(number) {
return this.registry[number];
}
/**
* @public
* @method getAll
* @description Get all the errors in the registry.
* @returns {object} - returns all the errors in the registry
*/
getAll() {
let temp = {};
for (let number in this.registry) {
temp[number] = this.registry[number];
}
return temp;
}
/**
* @private
* @method _initializeProperties
* @description Initializes the properties of the class.
*/
_initializeProperties() {
this.registry = {};
}
};
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Errors.html">Errors</a></li></ul><h3>Global</h3><ul><li><a href="global.html#constructor">constructor</a></li><li><a href="global.html#error">error</a></li><li><a href="global.html#getAll">getAll</a></li><li><a href="global.html#getAllErrors">getAllErrors</a></li><li><a href="global.html#getByNumber">getByNumber</a></li><li><a href="global.html#registerErrorWithNumber">registerErrorWithNumber</a></li><li><a href="global.html#setErrorWithNumber">setErrorWithNumber</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Nov 26 2015 17:49:03 GMT+0200 (FLE Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>