forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.exceptions.ts
More file actions
143 lines (132 loc) · 3.66 KB
/
Copy pathstellar.exceptions.ts
File metadata and controls
143 lines (132 loc) · 3.66 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpStatus,
} from "@nestjs/common";
import { Response } from "express";
import { Logger } from "@nestjs/common";
/**
* Base exception for Stellar-related errors
*/
export class StellarException extends Error {
constructor(
message: string,
public readonly code: string = "STELLAR_ERROR",
public readonly statusCode: number = HttpStatus.INTERNAL_SERVER_ERROR,
) {
super(message);
this.name = "StellarException";
}
}
/**
* Exception thrown when Stellar account is not found (404)
*/
export class StellarAccountNotFoundException extends StellarException {
constructor(publicKey: string) {
super(
`Account not found: ${publicKey}`,
"STELLAR_ACCOUNT_NOT_FOUND",
HttpStatus.NOT_FOUND,
);
this.name = "StellarAccountNotFoundException";
}
}
/**
* Exception thrown when payment/transaction is not found
*/
export class StellarPaymentNotFoundException extends StellarException {
constructor(memo?: string, transactionHash?: string) {
super(
`Payment not found${memo ? ` with memo: ${memo}` : ""}${transactionHash ? ` for transaction: ${transactionHash}` : ""}`,
"STELLAR_PAYMENT_NOT_FOUND",
HttpStatus.NOT_FOUND,
);
this.name = "StellarPaymentNotFoundException";
}
}
/**
* Exception thrown when Stellar address validation fails
*/
export class StellarAddressInvalidException extends StellarException {
constructor(
address: string,
addressType: "account" | "contract" = "account",
) {
super(
`Invalid Stellar ${addressType} address: ${address}`,
"STELLAR_ADDRESS_INVALID",
HttpStatus.BAD_REQUEST,
);
this.name = "StellarAddressInvalidException";
}
}
/**
* Exception thrown when Horizon API returns an error
*/
export class HorizonApiException extends StellarException {
constructor(
message: string,
public readonly horizonStatusCode: number,
public readonly originalError?: any,
) {
super(
message,
"HORIZON_API_ERROR",
horizonStatusCode === 429
? HttpStatus.TOO_MANY_REQUESTS
: horizonStatusCode >= 500
? HttpStatus.BAD_GATEWAY
: HttpStatus.INTERNAL_SERVER_ERROR,
);
this.name = "HorizonApiException";
}
}
/**
* Exception thrown when Soroban RPC call fails
*/
export class SorobanRpcException extends StellarException {
constructor(
message: string,
public readonly rpcCode?: number,
public readonly originalError?: any,
) {
super(message, "SOROBAN_RPC_ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
this.name = "SorobanRpcException";
}
}
/**
* Exception thrown when network configuration is invalid
*/
export class StellarNetworkConfigException extends StellarException {
constructor(message: string) {
super(
message,
"STELLAR_NETWORK_CONFIG_ERROR",
HttpStatus.INTERNAL_SERVER_ERROR,
);
this.name = "StellarNetworkConfigException";
}
}
/**
* Exception filter for Stellar exceptions
* Handles all Stellar-related exceptions and returns formatted responses
*/
@Catch(StellarException)
export class StellarExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(StellarExceptionFilter.name);
catch(exception: StellarException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
this.logger.error(
`Stellar exception: ${exception.code} - ${exception.message}`,
exception.stack,
);
response.status(exception.statusCode).json({
statusCode: exception.statusCode,
code: exception.code,
message: exception.message,
timestamp: new Date().toISOString(),
});
}
}