generated from jasonsturges/typescript-npm-package
-
Notifications
You must be signed in to change notification settings - Fork 7
/
handle-exceptions.ts
36 lines (32 loc) · 1.12 KB
/
handle-exceptions.ts
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
27
28
29
30
31
32
33
34
35
36
import { ErrorNoAdminKey, ErrorResponse, SpvWalletClient, SpvWalletError } from '../dist/typescript-npm-package.cjs.js';
import { exampleXPub } from './example-keys.js';
import { errMessage } from './utils.js';
const server = 'http://localhost:3003';
if (!exampleXPub) {
console.log(errMessage('xPub'));
process.exit(1);
}
try {
const client = new SpvWalletClient(server, {
xPub: exampleXPub,
});
//we're trying to make an admin request without adminKey
//the following line will throw ErrorNoAdminKey
const status = await client.AdminGetStatus();
console.log('Status:', status);
} catch (e) {
if (e instanceof SpvWalletError) {
// You can check the type of the error and do something specific
if (e instanceof ErrorResponse) {
console.error('Response status:', e.response.status);
console.error('Content:', e.content);
} else if (e instanceof ErrorNoAdminKey) {
console.error('ErrorNoAdminKey', e.message);
} else {
//check all the other error types here: src/errors.ts
console.error('SpvWalletError:', e.message);
}
} else {
console.log('Unknown error:', e);
}
}