From 09644d8ba8cede4c62b4f3029af1a0bc13863c98 Mon Sep 17 00:00:00 2001 From: Marco Ramos Date: Mon, 27 Mar 2017 07:26:35 -0400 Subject: [PATCH] Type Error Classes missing from json-patch Enforce stricter typings --- types/json-patch/index.d.ts | 34 ++++++++++++++++------------ types/json-patch/json-patch-tests.ts | 19 ++++++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/types/json-patch/index.d.ts b/types/json-patch/index.d.ts index bb70cff6c6..6f257924b0 100644 --- a/types/json-patch/index.d.ts +++ b/types/json-patch/index.d.ts @@ -4,36 +4,42 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace jsonpatch { - type OpPatch = AddPath | RemovePath | ReplacePath | MovePath | CopyPath | TestPath; + type OpPatch = AddPatch | RemovePatch | ReplacePatch | MovePatch | CopyPatch | TestPatch; interface Patch { - op: string; - } - interface AddPath extends Patch { path: string; + } + interface AddPatch extends Patch { + op: 'add'; value: any; } - interface RemovePath extends Patch { - path: string; + interface RemovePatch extends Patch { + op: 'remove'; } - interface ReplacePath extends Patch { - path: string; + interface ReplacePatch extends Patch { + op: 'replace'; value: any; } - interface MovePath extends Patch { + interface MovePatch extends Patch { + op: 'move'; from: string; - path: string; } - interface CopyPath extends Patch { + interface CopyPatch extends Patch { + op: 'copy'; from: string; - path: string; } - interface TestPath extends Patch { - path: string; + interface TestPatch extends Patch { + op: 'test'; value: any; } function apply(document: any, patches: OpPatch[]): any; function compile(patches: OpPatch[]): (document: any) => any; + + class JSONPatchError extends Error { } + class InvalidPointerError extends Error { } + class InvalidPatchError extends JSONPatchError { } + class PatchConflictError extends JSONPatchError { } + class PatchTestFailed extends Error { } } export = jsonpatch; diff --git a/types/json-patch/json-patch-tests.ts b/types/json-patch/json-patch-tests.ts index 3c3ff51f22..470b12fe54 100644 --- a/types/json-patch/json-patch-tests.ts +++ b/types/json-patch/json-patch-tests.ts @@ -32,3 +32,22 @@ jsonpatch.apply({ foo: 'bar' }, [{ op: 'test', path: '/foo', value: 'bar' }]); jsonpatch.compile([{ op: 'test', path: '/foo', value: 'bar' }])({ foo: 'bar' }); jp.apply({}, [{ op: 'add', path: '/foo', value: 'bar' }]); + +function patchShouldFail(document: any, patches: jsonpatch.OpPatch[]): string { + try { + jsonpatch.apply(document, patches); + throw new Error('Patch did not fail...'); + } catch (err) { + if (err instanceof jsonpatch.PatchTestFailed) { + return 'Test failed'; + } else if (err instanceof jsonpatch.InvalidPointerError) { + return 'Invalid Pointer'; + } else if (err instanceof jsonpatch.InvalidPatchError) { + return 'Invalid Patch'; + } else if (err instanceof jsonpatch.PatchConflictError) { + return 'Patch Conflict'; + } else { + return 'Failed with unknown error'; + } + } +} \ No newline at end of file