Skip to content

Commit

Permalink
refactor(errors): move isNativeError function
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Nov 8, 2024
1 parent 82d980e commit a6b6060
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
20 changes: 19 additions & 1 deletion packages/framework/src/errors/base.errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { HttpStatusEnum } from '../constants';
import { ErrorCodeEnum } from '../constants/error.constants';
import { isNativeError } from './guard.errors';

/**
* Check if the object is a native error.
*
* This method relies on `Object.prototype.toString()` behavior. It is possible to obtain
* an incorrect result when the object argument manipulates the `@@toStringTag` property.
*
* @param object - The object to check.
* @returns `true` if the object is a native error, `false` otherwise.
*/
export const isNativeError = (object: unknown): object is Error => {
if (typeof object !== 'object' || object === null) {
return false;
}

const proto = Object.getPrototypeOf(object);

return proto?.constructor?.name.endsWith('Error') ?? false;
};

/**
* Base error class.
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/errors/guard.errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect, it, describe } from 'vitest';
import { isFrameworkError, isNativeError, isPlatformError } from './guard.errors';
import { isFrameworkError, isPlatformError } from './guard.errors';
import { isNativeError, FrameworkError } from './base.errors';
import { PlatformError } from './platform.errors';
import { ErrorCodeEnum, HttpStatusEnum } from '../constants';
import { FrameworkError } from './base.errors';
import { BridgeError } from './bridge.errors';

class TestFrameworkError extends FrameworkError {
Expand Down
19 changes: 0 additions & 19 deletions packages/framework/src/errors/guard.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,6 @@ import { ErrorCodeEnum } from '../constants';
import { FrameworkError } from './base.errors';
import { PlatformError } from './platform.errors';

/**
* Check if the object is a native error.
*
* This method relies on `Object.prototype.toString()` behavior. It is possible to obtain
* an incorrect result when the object argument manipulates the `@@toStringTag` property.
*
* @param object - The object to check.
* @returns `true` if the object is a native error, `false` otherwise.
*/
export const isNativeError = (object: unknown): object is Error => {
if (typeof object !== 'object' || object === null) {
return false;
}

const proto = Object.getPrototypeOf(object);

return proto?.constructor?.name.endsWith('Error') ?? false;
};

/**
* Check if the error is a `FrameworkError`.
*
Expand Down

0 comments on commit a6b6060

Please sign in to comment.