Skip to content

Commit

Permalink
feat(runtime): move error guards and add getErrors util
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Nov 19, 2022
1 parent d0595e6 commit df67e0b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/runtime/src/guards/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { isAggregateError } from './isAggregateError';
export { isErrorWithCause } from './isErrorWithCause';
5 changes: 5 additions & 0 deletions packages/runtime/src/guards/isAggregateError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { isError } from 'lodash';

export function isAggregateError(maybeAggregateError: unknown): maybeAggregateError is Error & { errors: unknown[] } {
return isError(maybeAggregateError) && maybeAggregateError.constructor.name === 'AggregateError';
}
5 changes: 5 additions & 0 deletions packages/runtime/src/guards/isErrorWithCause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { isError } from 'lodash';

export function isErrorWithCause(maybeErrorWithCause: unknown): maybeErrorWithCause is Error & { cause: unknown } {
return isError(maybeErrorWithCause) && 'cause' in maybeErrorWithCause;
}
1 change: 1 addition & 0 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './guards';
export * from './utils';
export { default as fetch, DEFAULT_REQUEST_OPTIONS } from './fetch';
export * from './reader';
14 changes: 14 additions & 0 deletions packages/runtime/src/utils/getErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isAggregateError } from '../guards/isAggregateError';
import { isErrorWithCause } from '../guards/isErrorWithCause';

export function* getErrors(error: unknown): Iterable<unknown> {
if (isAggregateError(error)) {
for (const singleError of error.errors) {
yield* getErrors(singleError);
}
} else if (isErrorWithCause(error) && error.cause !== void 0) {
yield* getErrors(error.cause);
} else {
yield error;
}
}
1 change: 1 addition & 0 deletions packages/runtime/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './printError';
export * from './printPath';
export * from './printValue';
export * from './refs';
export { getErrors } from './getErrors';

0 comments on commit df67e0b

Please sign in to comment.