forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathisError.ts
More file actions
26 lines (22 loc) · 790 Bytes
/
isError.ts
File metadata and controls
26 lines (22 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import prettyFormat from 'pretty-format';
export default function isError(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
potentialError: any,
): {isError: boolean; message: string | null} {
// duck-type Error, see #2549
const isError =
potentialError !== null &&
typeof potentialError === 'object' &&
typeof potentialError.message === 'string' &&
typeof potentialError.name === 'string';
const message = isError
? null
: `Failed: ${prettyFormat(potentialError, {maxDepth: 3})}`;
return {isError, message};
}