Skip to content

Commit

Permalink
fix: implementation of adding custom keys to a problem object
Browse files Browse the repository at this point in the history
  • Loading branch information
imabp committed Aug 27, 2022
1 parent 77b0122 commit a954331
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const _testContext = new ProblemContextHelper();
describe("Class Methods Test Suite", () => {
test("Create Class with Custom Keys", () => {
const customKey = "RCA";
const testProblem = new Problem(_testContext._problemInstance, ["RCA"]);
_testContext._problemInstance[customKey]="Root Cause Analysis"
const testProblem = new Problem(_testContext._problemInstance);
expect(testProblem).toHaveProperty(customKey);
});

Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const DEFAULT_KEYS = [
"http",
"type",
"title",
"detail",
"instance",
"stack",
];
9 changes: 6 additions & 3 deletions src/problem.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_KEYS } from "./constants";
import {
httpObject,
ProblemInterface,
Expand Down Expand Up @@ -26,9 +27,11 @@ export class Problem extends Error implements ProblemInterface {
this.detail = problem.detail;
this.instance = problem.instance;
this.stack = problem.stack;
customKeys?.map((customKey) => {
this[customKey] = problem[customKey];
});

// add extra keys
Object.keys(problem)
.filter((el) => !DEFAULT_KEYS.includes(el))
.forEach((k) => (this[k] = problem[k]));
}

copy(problem: ProblemInterface, mode: COPY_MODE, props: string[]): Problem {
Expand Down

0 comments on commit a954331

Please sign in to comment.