Skip to content

Commit 634b477

Browse files
committed
Better printing of unknown errors in requests.
the-draupnir-project/Draupnir#733.
1 parent 66b580c commit 634b477

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Client/BotSDKBaseClient.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
} from '@the-draupnir-project/matrix-basic-types';
4848
import { resolveRoomReferenceSafe } from '../SafeMatrixClient';
4949
import { ResultError } from '@gnuxie/typescript-result';
50+
import util from 'util';
5051

5152
const log = new Logger('BotSDKBaseClient');
5253

@@ -80,10 +81,26 @@ function actionExceptionFromWeakError(
8081
}
8182

8283
function unknownError(error: unknown): never {
84+
const printedError = (() => {
85+
if (typeof error === 'object' && error !== null) {
86+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
87+
const toString = error.toString();
88+
if (toString !== '[object Object]') {
89+
return toString;
90+
}
91+
}
92+
try {
93+
return JSON.stringify(error);
94+
} catch {
95+
return util.inspect(error, {
96+
depth: 2,
97+
maxArrayLength: 10,
98+
breakLength: 80,
99+
});
100+
}
101+
})();
83102
throw new TypeError(
84-
// Not sure what to do yet other than throw?
85-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
86-
`What on earth are you throwing exactly? because it isn't an error ${error}`
103+
`What on earth are you throwing exactly? because it isn't an error: ${printedError}`
87104
);
88105
}
89106

0 commit comments

Comments
 (0)