-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json-pointer): use error hierarchy and metadata when throwing er…
- Loading branch information
Showing
11 changed files
with
169 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
import escape from './escape'; | ||
import CompilationJsonPointerError from './errors/CompilationJsonPointerError'; | ||
|
||
// compile :: String[] -> String | ||
const compile = (tokens: string[]): string => { | ||
if (tokens.length === 0) { | ||
return ''; | ||
} | ||
try { | ||
if (tokens.length === 0) { | ||
return ''; | ||
} | ||
|
||
return `/${tokens.map(escape).join('/')}`; | ||
return `/${tokens.map(escape).join('/')}`; | ||
} catch (error: unknown) { | ||
throw new CompilationJsonPointerError( | ||
'JSON Pointer compilation of tokens encountered an error.', | ||
{ | ||
tokens, | ||
}, | ||
); | ||
} | ||
}; | ||
|
||
export default compile; |
21 changes: 21 additions & 0 deletions
21
packages/apidom-json-pointer/src/errors/CompilationJsonPointerError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ApiDOMErrorOptions } from '@swagger-api/apidom-error'; | ||
|
||
import JsonPointerError from './JsonPointerError'; | ||
|
||
export interface CompilationJsonPointerErrorOptions extends ApiDOMErrorOptions { | ||
readonly tokens: string[]; | ||
} | ||
|
||
class CompilationJsonPointerError extends JsonPointerError { | ||
public readonly tokens!: string[]; | ||
|
||
constructor(message?: string, structuredOptions?: CompilationJsonPointerErrorOptions) { | ||
super(message, structuredOptions); | ||
|
||
if (typeof structuredOptions !== 'undefined') { | ||
this.tokens = [...structuredOptions.tokens]; | ||
} | ||
} | ||
} | ||
|
||
export default CompilationJsonPointerError; |
46 changes: 44 additions & 2 deletions
46
packages/apidom-json-pointer/src/errors/EvaluationJsonPointerError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
import { ApiDOMError } from '@swagger-api/apidom-error'; | ||
import { ApiDOMErrorOptions } from '@swagger-api/apidom-error'; | ||
import { Element, hasElementSourceMap, toValue } from '@swagger-api/apidom-core'; | ||
|
||
export default class EvaluationJsonPointerError extends ApiDOMError {} | ||
import JsonPointerError from './JsonPointerError'; | ||
|
||
export interface EvaluationJsonPointerErrorOptions<T extends Element> extends ApiDOMErrorOptions { | ||
readonly pointer: string; | ||
readonly tokens?: string[]; | ||
readonly failedToken?: string; | ||
readonly failedTokenPosition?: number; | ||
readonly element: T; | ||
} | ||
|
||
class EvaluationJsonPointerError<T extends Element> extends JsonPointerError { | ||
public readonly pointer!: string; | ||
|
||
public readonly tokens?: string[]; | ||
|
||
public readonly failedToken?: string; | ||
|
||
public readonly failedTokenPosition?: number; | ||
|
||
public readonly element!: string; | ||
|
||
public readonly elementSourceMap?: [[number, number, number], [number, number, number]]; | ||
|
||
constructor(message?: string, structuredOptions?: EvaluationJsonPointerErrorOptions<T>) { | ||
super(message, structuredOptions); | ||
|
||
if (typeof structuredOptions !== 'undefined') { | ||
this.pointer = structuredOptions.pointer; | ||
if (Array.isArray(structuredOptions.tokens)) { | ||
this.tokens = [...structuredOptions.tokens]; | ||
} | ||
this.failedToken = structuredOptions.failedToken; | ||
this.failedTokenPosition = structuredOptions.failedTokenPosition; | ||
this.element = structuredOptions.element.element; | ||
if (hasElementSourceMap(structuredOptions.element)) { | ||
this.elementSourceMap = toValue(structuredOptions.element.getMetaProperty('sourceMap')); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default EvaluationJsonPointerError; |
22 changes: 18 additions & 4 deletions
22
packages/apidom-json-pointer/src/errors/InvalidJsonPointerError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import { ApiDOMError } from '@swagger-api/apidom-error'; | ||
import { ApiDOMErrorOptions } from '@swagger-api/apidom-error'; | ||
|
||
export default class InvalidJsonPointerError extends ApiDOMError { | ||
constructor(pointer: string) { | ||
super(`Invalid JSON Pointer "${pointer}". Pointers must begin with "/"`); | ||
import JsonPointerError from './JsonPointerError'; | ||
|
||
export interface InvalidJsonPointerErrorOptions extends ApiDOMErrorOptions { | ||
readonly pointer: string; | ||
} | ||
|
||
class InvalidJsonPointerError extends JsonPointerError { | ||
public readonly pointer!: string; | ||
|
||
constructor(message?: string, structuredOptions?: InvalidJsonPointerErrorOptions) { | ||
super(message, structuredOptions); | ||
|
||
if (typeof structuredOptions !== 'undefined') { | ||
this.pointer = structuredOptions.pointer; | ||
} | ||
} | ||
} | ||
|
||
export default InvalidJsonPointerError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ApiDOMStructuredError } from '@swagger-api/apidom-error'; | ||
|
||
class JsonPointerError extends ApiDOMStructuredError {} | ||
|
||
export default JsonPointerError; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters