-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathErrorResourcesContext.ts
More file actions
62 lines (55 loc) · 2.08 KB
/
ErrorResourcesContext.ts
File metadata and controls
62 lines (55 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Resource } from 'rdf-object';
import type { IParamValueConflict } from '../preprocess/parameterproperty/ParameterPropertyHandlerRange';
/**
* An error that can include a context containing resources for display.
*/
export class ErrorResourcesContext extends Error {
public readonly context: IErrorContext;
public constructor(message: string, context: IErrorContext) {
super(message);
this.name = 'ErrorResourcesContext';
this.context = context;
}
public exportContext(): any {
return ErrorResourcesContext.contextToJson(this.context);
}
public static contextToJson(context: IErrorContext): any {
return Object.fromEntries(Object.entries(context)
.map(([ key, value ]) => {
let mapped: any;
if (typeof value === 'string') {
mapped = value;
} else if (Array.isArray(value)) {
mapped = value.map(valueSub => ErrorResourcesContext.resourceToJson(valueSub));
} else if (value instanceof Resource || value === undefined) {
mapped = ErrorResourcesContext.resourceToJson(value);
} else if ('description' in value) {
mapped = ErrorResourcesContext.conflictToJson(<IParamValueConflict> value);
} else {
mapped = ErrorResourcesContext.contextToJson(value);
}
return [ key, mapped ];
}));
}
public static resourceToJson(resource: Resource | undefined): any {
if (resource) {
return resource.toJSON(1);
}
}
public static conflictToJson(conflict: IParamValueConflict): any {
const data: any = { description: conflict.description };
if (conflict.causes) {
data.causes = [];
// Only pick the first 2 conflicts for visualization
for (const subConflict of conflict.causes.slice(0, 1)) {
data.causes.push(ErrorResourcesContext.conflictToJson(subConflict));
}
} else {
data.context = ErrorResourcesContext.contextToJson(conflict.context);
}
return data;
}
}
export interface IErrorContext {
[key: string]: Resource | Resource[] | string | undefined | IErrorContext | IParamValueConflict;
}