Skip to content

Commit

Permalink
Better isErr function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshelkov committed Jan 23, 2022
1 parent 9932228 commit 762b1d0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"error",
"error-handling"
],
"version": "1.0.20",
"version": "1.0.21",
"author": "Alex Shelkovskiy <alexshelkov@gmail.com>",
"repository": "https://github.com/alexshelkov/result",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion src/checks.ts
@@ -1,10 +1,14 @@
import { Err } from './err';
import { Failure, Success } from './result';

export const isErr = (input: unknown): input is Err => {
export const isUnknownErr = (input: unknown): input is Err => {
return typeof input === 'object' && input !== null && 'type' in input;
};

export const isErr = <Error = Err>(input: Error): input is Error extends Err ? Error : never => {
return isUnknownErr(input);
};

export const isErrType = <
Type extends string,
Fail,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -2,7 +2,7 @@ export { ErrUtil as Err, Err as ErrInfo, ErrLevel, Errs } from './err';

export { Success, Failure, Result, Response } from './result';

export { isErr, isErrType, isSuccessLike, isFailureLike } from './checks';
export { isUnknownErr, isErr, isErrType, isSuccessLike, isFailureLike } from './checks';

export {
ok,
Expand Down
11 changes: 9 additions & 2 deletions src/utils.ts
Expand Up @@ -9,7 +9,14 @@ import {
Response,
} from './result';

import { isPromise, isHaveStatus, isSuccessLike, isFailureLike, isErr } from './checks';
import {
isPromise,
isHaveStatus,
isSuccessLike,
isFailureLike,
isErr,
isUnknownErr,
} from './checks';

import { Err, ErrUtil } from './err';

Expand Down Expand Up @@ -56,7 +63,7 @@ const maybeFailToResult = <Data, Fail>(
prevResult.message,
prevResult.code
);
} else if (isErr(result)) {
} else if (isUnknownErr(result)) {
exception = new FailureException(
result.type,
result as unknown as Fail,
Expand Down

0 comments on commit 762b1d0

Please sign in to comment.