-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
29 lines (24 loc) · 882 Bytes
/
util.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
import * as T from "./types.ts";
export { guardAll, typeFromProps } from "./utils/propertiesMatcher.ts";
export function unwrapResultType<T>(res: T.ResultType<T>): T {
if ("error-code" in res) {
throw new Error(res["error-code"][1]);
} else if ("error" in res) {
throw new Error(res["error"]);
} else {
return res.result;
}
}
/** Used to check if the file exists before hand */
export const exists = (effects: T.Effects, props: { path: string; volumeId: string }) =>
effects.metadata(props).then(
(_) => true,
(_) => false
);
export const errorCode = (code: number, error: string) => ({
"error-code": [code, error] as const,
});
export const error = (error: string) => ({ error });
export const ok = { result: null };
export const isKnownError = (e: unknown): e is T.KnownError =>
e instanceof Object && ("error" in e || "error-code" in e);