版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

174 wiersze
7.4 KiB

  1. import { CallSite } from 'callsites';
  2. import { TypeGuard as TypeGuard$1 } from 'inferred-types';
  3. type TypeSubtype<T extends string = string, S extends string = string> = `${T}/${S}`;
  4. /**
  5. * Defines the structure of a [Brilliant Error](https://github.com/inocan-group/brilliant-errors).
  6. */
  7. interface IBrilliantError<N extends string, A extends string, T extends string, S extends string, H extends number, C extends ErrorConstructorType> extends Error {
  8. /** a static identifier to ensure that this is a brilliant error */
  9. kind: "BrilliantError";
  10. /** the **Name** of the error given when error was configured */
  11. name: N;
  12. /**
  13. * specifies the API which the error constructor will provide
  14. */
  15. constructorType: C;
  16. /**
  17. * The configured **App**'s name (this can be left undefined); alternatively
  18. * a library author may indicate the library name as being the "app".
  19. */
  20. app: A;
  21. /**
  22. * The **code** is the first segment of the "classification" string and
  23. * therefore the higher-level scope description of the error.
  24. */
  25. code: T;
  26. subType: S;
  27. /**
  28. * The representative HTTP status code associated with this error
  29. */
  30. httpStatus: C extends "network" ? H : H | undefined;
  31. /**
  32. * The classification of an error into a type/sub-type.
  33. *
  34. * Note: the "type" can also be found as an independant prop `code` for the
  35. * error. Likewise, the subType is defined as prop on the error as well.
  36. */
  37. classification: TypeSubtype<T, S>;
  38. /**
  39. * An array of stack elements instead of stringified version typically
  40. * returned.
  41. */
  42. structuredStack: CallSite[];
  43. /** the function (or class method) which the error occurred in */
  44. fn: string | null;
  45. filename: string | null;
  46. /** the line number where the error occurred */
  47. line: number | null;
  48. toJSON(): {
  49. app: A;
  50. name: string;
  51. message: string;
  52. classification: TypeSubtype<T, S>;
  53. httpStatus: H;
  54. code: T;
  55. subType: S;
  56. fn: string | null;
  57. line: number | null;
  58. };
  59. }
  60. declare abstract class BaseBrilliance<
  61. /** type */
  62. T extends string = string,
  63. /** sub-type */
  64. S extends string = string,
  65. /** http status codes */
  66. H extends number = number,
  67. /** name */
  68. N extends string = string,
  69. /** origin */
  70. O extends string = string> extends Error {
  71. readonly kind = "BrilliantError";
  72. /** The error's name */
  73. readonly name: N;
  74. /** the originating application or service */
  75. readonly origin: O;
  76. /**
  77. * The classification of an error into a type/sub-type
  78. */
  79. readonly classification: TypeSubtype<T, S>;
  80. /**
  81. * The major type from the error's classification
  82. */
  83. readonly type: T;
  84. /**
  85. * The minor type/subtype from the error's classification
  86. */
  87. readonly subType: S;
  88. /**
  89. * An HTTP numeric error code
  90. */
  91. readonly httpStatus?: H;
  92. readonly structuredStack: CallSite[];
  93. /** the function (or class method) which the error occurred in */
  94. readonly fn: string | null;
  95. readonly filename: string | null;
  96. /** the line number where the error occurred */
  97. readonly line: number | null;
  98. }
  99. /**
  100. * The constructor when using a "network" error from Brilliant Errors
  101. */
  102. type NetworkConstructor<T extends string, S extends string, H extends number, R extends any = void> = (code: H, message: string, options?: {
  103. classification?: TypeSubtype<T, S>;
  104. }) => R;
  105. type WrapperConstructor<T extends string, S extends string, H extends number, R extends any = void> = (underlying: Error, classification: TypeSubtype<T, S>, options?: {
  106. message?: string;
  107. httpErrorCode?: H;
  108. }) => R;
  109. type StandardConstructor<T extends string, S extends string, H extends number, R extends any = void> = (message: string, classification: TypeSubtype<T, S>, options?: IErrorRuntimeOptions<H> | undefined) => R;
  110. type ErrorConstructorType = "standard" | "network" | "wrapper";
  111. /** configuration options when setting up an Error class */
  112. type IErrorConfigOptions<T extends string, S extends string, C extends ErrorConstructorType> = {
  113. constructorType?: C;
  114. defaultHttpStatus?: number;
  115. requireHttpStatus?: boolean;
  116. defaultType?: T;
  117. defaultSubType?: S;
  118. };
  119. type IErrorRuntimeOptions<H extends number> = {
  120. httpStatusCode?: H;
  121. };
  122. type BrilliantErrorTuple<N extends string, A extends string, T extends string, S extends string, H extends number, C extends ErrorConstructorType> = [
  123. ConstructorFor<N, A, T, S, H, C>,
  124. TypeGuard$1<IBrilliantError<N, A, T, S, H, C>>,
  125. BrilliantErrorTypeGuard
  126. ];
  127. /**
  128. * Add any additional _options_ for the error type
  129. */
  130. type ErrorOptions<N extends string, A extends string, T extends string, S extends string, H extends number> = <C extends ErrorConstructorType = "standard">(options?: IErrorConfigOptions<T, S, C>) => BrilliantErrorTuple<N, A, T, S, H, C>;
  131. /** enter the numeric error codes that this error can throw; leaving it empty will allow all numeric codes */
  132. type ErrorHttpCodes<N extends string, A extends string, T extends string, S extends string> = <H extends number>(...httpCodes: H[]) => ErrorOptions<N, A, T, S, H>;
  133. type ErrorSubTypes<N extends string, A extends string, T extends string> = <S extends string>(...subTypes: S[]) => ErrorHttpCodes<N, A, T, S>;
  134. type ErrorTypes<N extends string, A extends string> = <T extends string>(...types: T[]) => ErrorSubTypes<N, A, T>;
  135. type IConstructorProps<N extends string, A extends string, T extends string, S extends string, H extends number> = {
  136. name: N;
  137. app: A;
  138. types: T[];
  139. subTypes: S[];
  140. httpCodes: H[];
  141. configOptions: IErrorConfigOptions<T, S, ErrorConstructorType>;
  142. };
  143. type ConstructorFor<N extends string, A extends string, T extends string, S extends string, H extends number, C extends ErrorConstructorType> = C extends "standard" ? Constructor<Parameters<StandardConstructor<T, S, H>>, IBrilliantError<N, A, T, S, H, C>> : C extends "network" ? Constructor<Parameters<NetworkConstructor<T, S, H>>, IBrilliantError<N, A, T, S, H, C>> : C extends "wrapper" ? Constructor<Parameters<WrapperConstructor<T, S, H>>, IBrilliantError<N, A, T, S, H, C>> : never;
  144. /**
  145. * Provides a default type if `T` is undefined
  146. */
  147. type IfUndefined<T, D extends T> = undefined extends T ? D : T;
  148. /** if `I` extends `T` then return as `T`, otherwise change type to `D` */
  149. type IfNotThen<I, T, D extends T> = I extends T ? I : D;
  150. /**
  151. * Define a class constructor; allowing strong typing for constructor's parameters
  152. * and the returned class structure.
  153. */
  154. type Constructor<Ctor extends any[], Klass extends any> = new (...props: Ctor) => Klass;
  155. type TypeGuard<T> = TypeGuard$1<T>;
  156. declare function isBrilliantError(error: unknown): error is IBrilliantError<any, any, any, any, any, any>;
  157. type BrilliantErrorTypeGuard = typeof isBrilliantError;
  158. /**
  159. * Create an error type and type guard using brilliant errors.
  160. */
  161. declare const createError: <N extends string, A extends string>(name: N, app: A) => ErrorTypes<N, A>;
  162. declare function prettyStack(s: CallSite[]): string;
  163. export { BaseBrilliance, BrilliantErrorTuple, BrilliantErrorTypeGuard, Constructor, ConstructorFor, ErrorConstructorType, ErrorHttpCodes, ErrorOptions, ErrorSubTypes, ErrorTypes, IBrilliantError, IConstructorProps, IErrorConfigOptions, IErrorRuntimeOptions, IfNotThen, IfUndefined, NetworkConstructor, StandardConstructor, TypeGuard, TypeSubtype, WrapperConstructor, createError, isBrilliantError, prettyStack };