aboutsummaryrefslogtreecommitdiff
path: root/src/utils/error.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/error.ts')
-rw-r--r--src/utils/error.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/utils/error.ts b/src/utils/error.ts
new file mode 100644
index 0000000..681db91
--- /dev/null
+++ b/src/utils/error.ts
@@ -0,0 +1,24 @@
+import _ from 'lodash';
+
+export { getErrorMessageIfThrows };
+
+function getErrorMessageIfThrows(cb: () => unknown) {
+ try {
+ cb();
+ return undefined;
+ } catch (err) {
+ if (_.isString(err)) {
+ return err;
+ }
+
+ if (_.isError(err)) {
+ return err.message;
+ }
+
+ if (_.isObject(err) && _.has(err, 'message')) {
+ return (err as { message: string }).message;
+ }
+
+ return 'An error as occurred.';
+ }
+}