-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy patherrors.ts
More file actions
161 lines (136 loc) · 4.46 KB
/
errors.ts
File metadata and controls
161 lines (136 loc) · 4.46 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { getDbErrorCode } from './executor/error-processor';
/**
* Reason code for ORM errors.
*/
export enum ORMErrorReason {
/**
* ORM client configuration error.
*/
CONFIG_ERROR = 'config-error',
/**
* Invalid input error.
*/
INVALID_INPUT = 'invalid-input',
/**
* The specified record was not found.
*/
NOT_FOUND = 'not-found',
/**
* Operation is rejected by access policy.
*/
REJECTED_BY_POLICY = 'rejected-by-policy',
/**
* Error was thrown by the underlying database driver.
*/
DB_QUERY_ERROR = 'db-query-error',
/**
* The requested operation is not supported.
*/
NOT_SUPPORTED = 'not-supported',
/**
* An internal error occurred.
*/
INTERNAL_ERROR = 'internal-error',
}
/**
* Reason code for policy rejection.
*/
export enum RejectedByPolicyReason {
/**
* Rejected because the operation is not allowed by policy.
*/
NO_ACCESS = 'no-access',
/**
* Rejected because the result cannot be read back after mutation due to policy.
*/
CANNOT_READ_BACK = 'cannot-read-back',
/**
* Other reasons.
*/
OTHER = 'other',
}
/**
* ZenStack ORM error.
*/
export class ORMError extends Error {
constructor(
public reason: ORMErrorReason,
message?: string,
options?: ErrorOptions,
) {
super(message, options);
}
/**
* The name of the model that the error pertains to.
*/
public model?: string;
/**
* The error code given by the underlying database driver.
*/
public dbErrorCode?: unknown;
/**
* The error message given by the underlying database driver.
*/
public dbErrorMessage?: string;
/**
* The reason code for policy rejection. Only available when `reason` is `REJECTED_BY_POLICY`.
*/
public rejectedByPolicyReason?: RejectedByPolicyReason;
/**
* Custom error codes from every policy rule that contributed to this rejection.
* Set via the optional third argument of `@@allow` / `@@deny`. Only available when
* `reason` is `REJECTED_BY_POLICY` and at least one matching rule carries a code.
* Note: surfaced for `create`, `post-update`, `update`, `delete`, and single-row `read`
* violations. For `read`, only `findFirst`/`findUnique`-equivalent queries (LIMIT 1)
* where a denied row exists will throw; `findMany` uses filter-based enforcement.
*/
public policyCodes?: string[];
/**
* The SQL query that was executed. Only available when `reason` is `DB_QUERY_ERROR`.
*/
public sql?: string;
/**
* The parameters used in the SQL query. Only available when `reason` is `DB_QUERY_ERROR`.
*/
public sqlParams?: readonly unknown[];
}
export function createConfigError(message: string, options?: ErrorOptions) {
return new ORMError(ORMErrorReason.CONFIG_ERROR, message, options);
}
export function createNotFoundError(model: string, message?: string, options?: ErrorOptions) {
const error = new ORMError(ORMErrorReason.NOT_FOUND, message ?? 'Record not found', options);
error.model = model;
return error;
}
export function createInvalidInputError(message: string, model?: string, options?: ErrorOptions) {
const error = new ORMError(ORMErrorReason.INVALID_INPUT, message, options);
error.model = model;
return error;
}
export function createDBQueryError(message: string, dbError: unknown, sql: string, parameters: readonly unknown[]) {
const error = new ORMError(ORMErrorReason.DB_QUERY_ERROR, message, { cause: dbError });
error.dbErrorCode = getDbErrorCode(dbError);
error.dbErrorMessage = dbError instanceof Error ? dbError.message : undefined;
error.sql = sql;
error.sqlParams = parameters;
return error;
}
export function createRejectedByPolicyError(
model: string,
reason: RejectedByPolicyReason,
message: string,
options?: ErrorOptions,
) {
const error = new ORMError(ORMErrorReason.REJECTED_BY_POLICY, message, options);
error.model = model;
error.rejectedByPolicyReason = reason;
return error;
}
export function createNotSupportedError(message: string, options?: ErrorOptions) {
return new ORMError(ORMErrorReason.NOT_SUPPORTED, message, options);
}
export function createInternalError(message: string, model?: string, options?: ErrorOptions) {
const error = new ORMError(ORMErrorReason.INTERNAL_ERROR, message, options);
error.model = model;
return error;
}