1 import type { DocsApiErrorCode } from '@proton/shared/lib/api/docs'
3 export type DocsApiError = {
8 export class ApiResult<T> {
10 private isSuccess: boolean,
11 private error?: DocsApiError,
18 return !this.isSuccess
22 if (!this.isSuccess) {
23 throw new Error(`Cannot get value of an unsuccessful result: ${this.error}`)
26 return this.value as T
29 getError(): DocsApiError {
30 if (this.isSuccess || this.error === undefined) {
31 throw new Error('Cannot get an error of a successful result')
37 getErrorMessage(): string {
38 if (this.isSuccess || this.error === undefined) {
39 throw new Error('Cannot get an error message of a successful result')
42 return this.error.message
45 static ok<U>(value?: U): ApiResult<U> {
46 return new ApiResult<U>(true, undefined, value)
49 static fail<U>(error: DocsApiError): ApiResult<U> {
51 throw new Error('Attempting to create a failed result without an error')
54 return new ApiResult<U>(false, error)