/
errors.ts
144 lines (123 loc) · 3.78 KB
/
errors.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { repr } from './helpers';
import type { Class } from './types';
/** Base class for all Oso errors. */
export class OsoError extends Error {
constructor(message?: string) {
// If we ever switch to supporting ES5, we'll have to make sure that
// OsoError prototypes are properly getting set.
// See: https://stackoverflow.com/questions/41102060/typescript-extending-error-class/48342359#48342359
super(message);
}
}
export class AuthorizationError extends OsoError {}
export class NotFoundError extends AuthorizationError {
constructor() {
super(
'Oso NotFoundError -- The current user does not have permission to read the given resource. You should handle this error by returning a 404 error to the client.'
);
}
}
export class ForbiddenError extends AuthorizationError {
constructor() {
super(
'Oso ForbiddenError -- The requested action was not allowed for the given resource. You should handle this error by returning a 403 error to the client.'
);
}
}
/** Base error type. */
export class PolarError extends OsoError {}
export class DuplicateClassAliasError extends PolarError {
constructor({
name,
cls,
existing,
}: {
name: string;
cls: Class;
existing: Class;
}) {
super(
`Attempted to alias ${cls.name} as '${name}', but ${existing.name} already has that alias.`
);
}
}
export class DuplicateInstanceRegistrationError extends PolarError {
constructor(id: number) {
super(
`Attempted to register instance ${id}, but an instance with that ID already exists.`
);
}
}
export class InlineQueryFailedError extends PolarError {
constructor(source: string) {
super(`Inline query failed: ${source}`);
}
}
export class InvalidAttributeError extends PolarError {
constructor(instance: unknown, field: string) {
super(`${field} not found on ${repr(instance)}.`);
}
}
export class InvalidCallError extends PolarError {
constructor(instance: unknown, field: string) {
super(`${repr(instance)}.${field} is not a function`);
}
}
export class InvalidIteratorError extends PolarError {
constructor(term: unknown) {
super(`${repr(term)} is not iterable`);
}
}
export class InvalidConstructorError extends PolarError {
constructor(ctor: unknown) {
super(`${repr(ctor)} is not a constructor`);
}
}
export class InvalidQueryEventError extends PolarError {
constructor(event: string) {
super(`Invalid query event: ${event}`);
}
}
export class KwargsError extends PolarError {
constructor() {
super('JavaScript does not support keyword arguments');
}
}
export class PolarFileExtensionError extends PolarError {
constructor(file: string) {
super(`Polar files must have .polar extension. Offending file: ${file}`);
}
}
export class PolarFileNotFoundError extends PolarError {
constructor(file: string) {
super(`Could not find file: ${file}`);
}
}
export class UnregisteredClassError extends PolarError {
constructor(name: string) {
super(`Unregistered class: ${name}.`);
}
}
export class UnregisteredInstanceError extends PolarError {
constructor(id: number) {
super(`Unregistered instance: ${id}.`);
}
}
export class DataFilteringConfigurationError extends PolarError {
constructor() {
super(
"Missing 'adapter' implementation. Did you forget to call `Oso.setDataFilteringAdapter()`?"
);
}
}
export class UnexpectedExpressionError extends PolarError {
constructor() {
super(
`Received Expression from Polar VM. The Expression type is only supported when
using data filtering features. Did you perform an
operation over an unbound variable in your policy?
To silence this error and receive an Expression result, pass the
\`{ acceptExpression: true }\` option to \`Oso.query()\` or \`Oso.queryRule()\`.`
);
}
}