版博士V2.0程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Brilliant Errors
  2. > **Note:** the version 6.x of this library had moved to newer dependencies wand the `callsites` library _only_ support the ESM module system these days but because we were packaging up in both ESM and CJS it was causing errors. I had intended to keep CJS around for a while longer but in most cases you really should be preferring ESM anyway. In version **7.x** and going forward we are only publishing version ESM. If you still need CJS you can use the latest `0.5.x` release.
  3. The base errors you get from Javascript leave a lot to be desired and this library attempts to provide a _consistent_ way to provide solid meta information on errors raised in Typescript/Javascript projects.
  4. This library provides two _configurators_ for errors:
  5. 1. `createError` - the default error type provides an Error with strongly typed classification but does not require an HTTP status code for errors (you _are_ allowed to add the HTTP status code)
  6. ```ts
  7. type ErrorType = "not-allowed" | "unexpected" | "missing-info";
  8. const MyError = createError<ErrorType>(e => e.name("MyError").origin("my-app"));
  9. // instantiation
  10. const error = new MyError("I've fallen and I can't get up", "unexpected/old-age");
  11. // static initializers for edge cases
  12. const error = MyError.withUnderlying(err, "No really, I can't get up", "unexpected/fer-fucks-sake");
  13. const error = MyError.withHttpCode(500, "Not worth repeating", "unexpected/web-fall");
  14. // options hash (means you can get to everything if you need it)
  15. const error = new MyError("Just stop it", "not-allowed/shit-happens", { underlying: err, httpStatus: 500 });
  16. ```
  17. 2. `createHttpError` - an HTTP error produces errors which are guarenteed to have an HTTP status code as part of their payload but they also follow the Type/Subtype conventions in the base error.
  18. ```ts
  19. const HttpError = createHttpError<ErrorType, SubType>(e =>
  20. e.name("HttpError")
  21. .origin("my-network-app")
  22. .defaultType("missing-info")
  23. );
  24. // instantiotion
  25. const error = new HttpError(403, "couldn't find member's id", "no-membership-id");
  26. All errors have the following attributes:
  27. - `kind` - a string literal type for the error family the error originates from
  28. - `name` - a string literal type for the error name
  29. - `origin` - a string literal type for the application/service which originated this error
  30. - `classification` - strongly typed Type/Subtype system
  31. - `httpStatus` - a numeric HTTP status code that can (and in some cases must) be set
  32. - `underlying` - all errors _can_ store an underlying error and the wrapper error requires it
  33. They also have brilliant `.toJSON()` and `message` outputs which shine above the plain old vanilla JS error.
  34. For a repo which wants to use these configurators, you will create a file in your repo for the new error and then configure it something like this:
  35. `src/error/MyError.ts`:
  36. ```ts
  37. export default MyError = createLibraryError('MyError', { ... });
  38. ```
  39. All options in the configurators are strongly typed which includes full documentatory information so rather than try to repeat that here we emplore you to use the _built-in_ Typescript documentation within your code editor of choice.