Skip to content

Commit

Permalink
Added previous parameter to FailureReason.reset function
Browse files Browse the repository at this point in the history
  • Loading branch information
bryaningl3 committed Jun 28, 2023
1 parent 67c9124 commit 951f982
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .releases/4.29.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**New Features**

* Added optional parameter to the `FailureReason.reset` function.

**Other**

* Modified the output of the `FailureReason.format` function.
35 changes: 23 additions & 12 deletions api/failures/FailureReason.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = (() => {
'use strict';

/**
* Describes all of the reasons for API failure. Since there can be multiple reasons, the reasons are
* stored in a tree structure.
* Describes all of the reasons for API failure. Since there can be multiple
* reasons, the reasons are stored in a tree structure.
*
* @public
* @param {Object=} data - Data regarding the API request itself, likely independent of the failure data (which is maintained in the tree structure).
Expand All @@ -20,8 +20,8 @@ module.exports = (() => {
constructor(data) {
this._data = data || null;

this._head = new Tree();
this._current = this._head;
this._root = new Tree();
this._current = this._root;
}

/**
Expand Down Expand Up @@ -50,10 +50,21 @@ module.exports = (() => {
* Resets the current node to the head of the tree.
*
* @public
* @param {Boolean=} previous
* @returns {FailureReason} - The current instance, allowing for method chaining.
*/
reset() {
this._current = this._head;
reset(previous) {
assert.argumentIsOptional(previous, 'previous', Boolean);

let node;

if (previous && this._current.getIsInner()) {
node = this._current.getParent();
} else {
node = this._root;
}

this._current = node;

return this;
}
Expand All @@ -65,7 +76,7 @@ module.exports = (() => {
* @returns {Array}
*/
format() {
const reasons = this._head.toJSObj((item) => {
const reasons = this._root.toJSObj((item) => {
const formatted = { };

formatted.code = item ? item.type.code : null;
Expand All @@ -76,7 +87,7 @@ module.exports = (() => {
}

return formatted;
});
}, true);

return reasons.children;
}
Expand All @@ -92,7 +103,7 @@ module.exports = (() => {
hasFailureType(type) {
assert.argumentIsRequired(type, 'type', FailureType, 'FailureType');

return this._head.search(item => item.type === type, false, false) !== null;
return this._root.search(item => item.type === type, false, false) !== null;
}

/**
Expand All @@ -103,7 +114,7 @@ module.exports = (() => {
* @returns {Boolean}
*/
getIsSevere() {
return this._head.search(item => item.type.severe, false, false) !== null;
return this._root.search(item => item.type.severe, false, false) !== null;
}

/**
Expand All @@ -114,7 +125,7 @@ module.exports = (() => {
* @returns {Number|null}
*/
getErrorCode() {
const node = this._head.search(item => item.type.error !== null, true, false);
const node = this._root.search(item => item.type.error !== null, true, false);

if (node !== null) {
return node.getValue().type.error;
Expand Down Expand Up @@ -166,7 +177,7 @@ module.exports = (() => {

let returnVal = null;

reason._head.walk((item) => {
reason._root.walk((item) => {
let code = FailureType.getHttpStatusCode(item.type);

if (returnVal === null || returnVal !== 400) {
Expand Down

0 comments on commit 951f982

Please sign in to comment.