-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinks-error.ts
More file actions
73 lines (60 loc) · 2.16 KB
/
links-error.ts
File metadata and controls
73 lines (60 loc) · 2.16 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
63
64
65
66
67
68
69
70
71
72
73
import {
NICE_ERROR_TYPES,
ValidationError,
createNiceErrorMessage,
uniqueErrorId,
} from './validation-error';
import isPlainObject from '../utils/is-plain-object';
import typeOf from '../utils/type-of';
import { Value } from 'json-typescript';
import { Document } from 'jsonapi-typescript';
export const LINKS_ERROR_TYPES = {
UNKNOWN_MEMBER: uniqueErrorId(),
INVALID_MEMBER: uniqueErrorId(),
VALUE_MUST_BE_OBJECT: uniqueErrorId(),
OBJECT_MUST_NOT_BE_EMPTY: uniqueErrorId(),
INVALID_SELF: uniqueErrorId(),
INVALID_RELATED: uniqueErrorId(),
INVALID_PAGINATION: uniqueErrorId(),
};
export interface ILinksErrorOptions {
value: Value;
path: string;
document: Document;
code: string | number;
member: any;
}
export class LinksError extends ValidationError {
constructor(options: ILinksErrorOptions) {
let { value, path, document } = options;
const errorLocation = createNiceErrorMessage({
key: Array.isArray(value) ? '' : value,
value: isPlainObject(document) ? JSON.stringify(document) : document,
path,
code: NICE_ERROR_TYPES.OBJECT_ERROR,
});
const error = buildMetaErrorMessage(options);
const message = error + errorLocation;
super(message);
}
}
function buildMetaErrorMessage(options: ILinksErrorOptions) {
let { value, code, member, path } = options;
switch (code) {
case LINKS_ERROR_TYPES.INVALID_MEMBER:
return `'${path}.${member}' MUST NOT be undefined.`;
case LINKS_ERROR_TYPES.VALUE_MUST_BE_OBJECT:
return `'${path}.${member}' MUST be an object when present: found value of type ${typeOf(
value
)}`;
case LINKS_ERROR_TYPES.OBJECT_MUST_NOT_BE_EMPTY:
return `'${path}.${member}' MUST have at least one member: found an empty object.`;
case LINKS_ERROR_TYPES.INVALID_SELF:
return `'${path}.${member}' MUST contain self as string URLs or an object`;
case LINKS_ERROR_TYPES.INVALID_RELATED:
return `'${path}.${member}' MUST contain related as string URLs or an object`;
case LINKS_ERROR_TYPES.INVALID_PAGINATION:
return `'${path}.${member}' included pagination MUST be null, string URL or an object`;
}
return 'DocumentError';
}