|  | 
|  | 1 | +/** | 
|  | 2 | + * [RFC] We need a better error system #159 | 
|  | 3 | + *  https://github.com/wechaty/puppet/issues/159 | 
|  | 4 | + */ | 
|  | 5 | +import { log } from '../config.js' | 
|  | 6 | + | 
|  | 7 | +import type { EcmaError }   from './ecma.js' | 
|  | 8 | +import { isEcmaError }      from './ecma.js' | 
|  | 9 | +import type { GrpcStatus }  from './grpc.js' | 
|  | 10 | +import { | 
|  | 11 | +  isGrpcStatus, | 
|  | 12 | +  Code, | 
|  | 13 | +}                           from './grpc.js' | 
|  | 14 | + | 
|  | 15 | +const isGError = (payload: any): payload is GError => payload instanceof Object | 
|  | 16 | +  && isEcmaError(payload) | 
|  | 17 | +  && isGrpcStatus(payload) | 
|  | 18 | + | 
|  | 19 | +class GError extends Error implements GrpcStatus, EcmaError { | 
|  | 20 | + | 
|  | 21 | +  code     : number | 
|  | 22 | +  details? : any[] | 
|  | 23 | + | 
|  | 24 | +  /** | 
|  | 25 | +   * From a gRPC standard error | 
|  | 26 | +   *  Protobuf | 
|  | 27 | +   */ | 
|  | 28 | +  public static fromJSON (payload: string | GrpcStatus | EcmaError) { | 
|  | 29 | +    log.verbose('PuppetError', 'from("%s")', JSON.stringify(payload)) | 
|  | 30 | + | 
|  | 31 | +    if (typeof payload === 'string') { | 
|  | 32 | +      payload = JSON.parse(payload) | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    if (!isEcmaError(payload) && !isGrpcStatus(payload)) { | 
|  | 36 | +      throw new Error('payload is neither EcmaError nor GrpcStatus') | 
|  | 37 | +    } | 
|  | 38 | + | 
|  | 39 | +    const e = new this(payload) | 
|  | 40 | +    return e | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  protected constructor ( | 
|  | 44 | +    payload: GrpcStatus | EcmaError, | 
|  | 45 | +  ) { | 
|  | 46 | +    super() | 
|  | 47 | +    log.verbose('PuppetError', 'constructor("%s")', JSON.stringify(payload)) | 
|  | 48 | + | 
|  | 49 | +    /** | 
|  | 50 | +     * Common properties | 
|  | 51 | +     */ | 
|  | 52 | +    this.message = payload.message | 
|  | 53 | + | 
|  | 54 | +    if (isGError(payload)) { | 
|  | 55 | +      this.code    = payload.code | 
|  | 56 | +      this.details = payload.details | 
|  | 57 | +      this.name    = payload.name | 
|  | 58 | +      this.stack   = payload.stack | 
|  | 59 | + | 
|  | 60 | +    } else if (isGrpcStatus(payload)) { | 
|  | 61 | +      this.code    = payload.code | 
|  | 62 | +      this.details = payload.details | 
|  | 63 | +      /** | 
|  | 64 | +       * Convert gRPC error to EcmaError | 
|  | 65 | +       */ | 
|  | 66 | +      this.name = Array.isArray(payload.details) && payload.details.length > 0 | 
|  | 67 | +        ? payload.details[0] | 
|  | 68 | +        : Code[this.code] || String(this.code) | 
|  | 69 | + | 
|  | 70 | +    } else if (isEcmaError(payload)) { | 
|  | 71 | +      this.name  = payload.name | 
|  | 72 | +      this.stack = payload.stack | 
|  | 73 | +      /** | 
|  | 74 | +       * Convert EcmaError to gRPC error | 
|  | 75 | +       */ | 
|  | 76 | +      this.code  = Code.UNKNOWN | 
|  | 77 | +      this.details = [ | 
|  | 78 | +        payload.name, | 
|  | 79 | +        ...payload.stack?.split('\n') ?? [], | 
|  | 80 | +      ] | 
|  | 81 | + | 
|  | 82 | +    } else { | 
|  | 83 | +      throw new Error('payload is neither EcmaError nor GrpcStatus') | 
|  | 84 | +    } | 
|  | 85 | +  } | 
|  | 86 | + | 
|  | 87 | +  public toJSON (): GrpcStatus & EcmaError { | 
|  | 88 | +    return { | 
|  | 89 | +      code    : this.code, | 
|  | 90 | +      details : this.details, | 
|  | 91 | +      message : this.message, | 
|  | 92 | +      name    : this.name, | 
|  | 93 | +      stack   : this.stack, | 
|  | 94 | +    } | 
|  | 95 | +  } | 
|  | 96 | + | 
|  | 97 | +  public toGrpcStatus (): GrpcStatus { | 
|  | 98 | +    return { | 
|  | 99 | +      code    : this.code, | 
|  | 100 | +      details : this.details, | 
|  | 101 | +      message : this.message, | 
|  | 102 | +    } | 
|  | 103 | +  } | 
|  | 104 | + | 
|  | 105 | +  public toEcmaError (): EcmaError { | 
|  | 106 | +    return { | 
|  | 107 | +      message : this.message, | 
|  | 108 | +      name    : this.name, | 
|  | 109 | +      stack   : this.stack, | 
|  | 110 | +    } | 
|  | 111 | +  } | 
|  | 112 | + | 
|  | 113 | +} | 
|  | 114 | + | 
|  | 115 | +export { | 
|  | 116 | +  GError, | 
|  | 117 | +  isGError, | 
|  | 118 | +  isGrpcStatus, | 
|  | 119 | +  isEcmaError, | 
|  | 120 | +} | 
0 commit comments