Guest User

Untitled

a guest
Jan 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import { any } from 'ramda';
  2.  
  3. class DbError extends Error {
  4. constructor(message: string) {
  5. super(message);
  6. // Set the prototype explicitly.
  7. Object.setPrototypeOf(this, DbError.prototype);
  8. }
  9. }
  10.  
  11. class CacheError extends Error {
  12. constructor(message: string) {
  13. super(message);
  14. // Set the prototype explicitly.
  15. Object.setPrototypeOf(this, CacheError.prototype);
  16. }
  17. }
  18.  
  19. class CertError extends Error {
  20. constructor(message: string) {
  21. super();
  22. this.message = message;
  23. }
  24. }
  25.  
  26. type BadRequest = DbError | CacheError;
  27. type Forbidden = CertError;
  28. const badRequestTypes = [DbError, CacheError];
  29.  
  30. function isBadRequest(x: any): x is BadRequest {
  31. return any(c => x instanceof c, badRequestTypes);
  32. }
  33.  
  34. const dbError = new DbError('DbError');
  35. const cacheError = new CacheError('CacheError');
  36. const certError = new CertError('CertError');
  37.  
  38. console.log(dbError instanceof DbError);
  39. console.log(cacheError instanceof CacheError);
  40. console.log(certError instanceof DbError);
  41. console.log(`isBadRequest: ${isBadRequest(dbError)}`);
  42. console.log(`isBadRequest: ${isBadRequest(cacheError)}`);
  43. // console.log(typeof certError);
  44. // console.log("kk");
Add Comment
Please, Sign In to add comment