Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { any } from 'ramda';
- class DbError extends Error {
- constructor(message: string) {
- super(message);
- // Set the prototype explicitly.
- Object.setPrototypeOf(this, DbError.prototype);
- }
- }
- class CacheError extends Error {
- constructor(message: string) {
- super(message);
- // Set the prototype explicitly.
- Object.setPrototypeOf(this, CacheError.prototype);
- }
- }
- class CertError extends Error {
- constructor(message: string) {
- super();
- this.message = message;
- }
- }
- type BadRequest = DbError | CacheError;
- type Forbidden = CertError;
- const badRequestTypes = [DbError, CacheError];
- function isBadRequest(x: any): x is BadRequest {
- return any(c => x instanceof c, badRequestTypes);
- }
- const dbError = new DbError('DbError');
- const cacheError = new CacheError('CacheError');
- const certError = new CertError('CertError');
- console.log(dbError instanceof DbError);
- console.log(cacheError instanceof CacheError);
- console.log(certError instanceof DbError);
- console.log(`isBadRequest: ${isBadRequest(dbError)}`);
- console.log(`isBadRequest: ${isBadRequest(cacheError)}`);
- // console.log(typeof certError);
- // console.log("kk");
Add Comment
Please, Sign In to add comment