@concordiq/error-sdk 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readMe.md +155 -1
- package/src/client.js +23 -1
- package/src/handlers.js +45 -11
- package/src/index.js +83 -14
package/package.json
CHANGED
package/readMe.md
CHANGED
|
@@ -54,16 +54,170 @@ import express from "express";
|
|
|
54
54
|
import ErrorSDK from "@concordiq/error-sdk";
|
|
55
55
|
----
|
|
56
56
|
const app = express();
|
|
57
|
-
|
|
57
|
+
``
|
|
58
58
|
app.use("/pay", (req, res) => {
|
|
59
59
|
throw new Error("Payment failed");
|
|
60
60
|
});
|
|
61
|
+
``
|
|
61
62
|
|
|
62
63
|
/**
|
|
63
64
|
* Must be last middleware
|
|
64
65
|
*/
|
|
66
|
+
|
|
67
|
+
``
|
|
68
|
+
app.use(ErrorSDK.expressMiddleware());
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
✅ Default Import (Most Common)
|
|
73
|
+
import ErrorSDK from "@concordiq/error-sdk";
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
ErrorSDK.init({...});
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
✅ Named Import (Tree-shaking)
|
|
81
|
+
import { init, capture } from "@concordiq/error-sdk";
|
|
82
|
+
|
|
83
|
+
init({...});
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
✅ CommonJS
|
|
88
|
+
const ErrorSDK = require("@concordiq/error-sdk");
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
✅ Dynamic Import
|
|
93
|
+
const sdk = await import("@concordiq/error-sdk");
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
#
|
|
99
|
+
import express, {
|
|
100
|
+
Request,
|
|
101
|
+
Response,
|
|
102
|
+
NextFunction
|
|
103
|
+
} from "express";
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
import ErrorSDK from "@concordiq/error-sdk";
|
|
108
|
+
|
|
109
|
+
const app = express();
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Initialize once (on app startup)
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
ErrorSDK.init({
|
|
117
|
+
apiKey: "coiq-6ddfeede1d57a268f7131ef0597624200a8a02b7",
|
|
118
|
+
|
|
119
|
+
endpoint: "https://concordiq.onrender.com/api/v1/ingest/events",
|
|
120
|
+
|
|
121
|
+
env: "production",
|
|
122
|
+
|
|
123
|
+
service: "payment-api"
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Express error handler (optional custom)
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
export function expressHandler() {
|
|
133
|
+
return (
|
|
134
|
+
err: Error,
|
|
135
|
+
req: Request,
|
|
136
|
+
res: Response,
|
|
137
|
+
next: NextFunction
|
|
138
|
+
) => {
|
|
139
|
+
ErrorSDK.capture(err, {
|
|
140
|
+
url: req.originalUrl,
|
|
141
|
+
method: req.method,
|
|
142
|
+
user: (req as any).user?.id
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
next(err);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Register middleware (MUST be last)
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
|
|
65
154
|
app.use(ErrorSDK.expressMiddleware());
|
|
66
155
|
|
|
156
|
+
console.log("ErrorSDK loaded:", ErrorSDK);
|
|
157
|
+
#
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
`✅ Basic Usage in a Controller/Services (Recommended Way):`
|
|
162
|
+
``using this SDK inside controllers/services is actually the best practice, especially for business-logic errors.``
|
|
163
|
+
|
|
164
|
+
import { Request, Response, NextFunction } from "express";
|
|
165
|
+
import ErrorSDK from "@concordiq/error-sdk";
|
|
166
|
+
|
|
167
|
+
export const createPayment = async (
|
|
168
|
+
req: Request,
|
|
169
|
+
res: Response,
|
|
170
|
+
next: NextFunction
|
|
171
|
+
) => {
|
|
172
|
+
try {
|
|
173
|
+
// Your logic
|
|
174
|
+
const { amount } = req.body;
|
|
175
|
+
|
|
176
|
+
if (!amount) {
|
|
177
|
+
throw new Error("Amount is required");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Simulate error
|
|
181
|
+
if (amount < 100) {
|
|
182
|
+
throw new Error("Minimum payment is 100");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
res.status(201).json({
|
|
186
|
+
success: true,
|
|
187
|
+
message: "Payment created"
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
} catch (err) {
|
|
191
|
+
/**
|
|
192
|
+
* Capture error manually
|
|
193
|
+
*/
|
|
194
|
+
ErrorSDK.capture(err as Error, {
|
|
195
|
+
controller: "createPayment",
|
|
196
|
+
user: (req as any).user?.id,
|
|
197
|
+
body: req.body
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Pass to global handler
|
|
202
|
+
*/
|
|
203
|
+
next(err);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
`✅ Why This SDK Is Best for you:`
|
|
209
|
+
|
|
210
|
+
`This gives you:`
|
|
211
|
+
|
|
212
|
+
`✔ Controller name`
|
|
213
|
+
`✔ User info`
|
|
214
|
+
`✔ Request data`
|
|
215
|
+
`✔ Business context`
|
|
216
|
+
`✔ Stack trace`
|
|
217
|
+
`✔ Global error handling`
|
|
218
|
+
|
|
219
|
+
`Your dashboard becomes very useful.`
|
|
220
|
+
|
|
67
221
|
|
|
68
222
|
|
|
69
223
|
## Features:
|
package/src/client.js
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
// import fetch from "node-fetch";
|
|
2
|
+
|
|
3
|
+
// export async function sendError(apiKey, url, payload) {
|
|
4
|
+
// try {
|
|
5
|
+
// await fetch(url, {
|
|
6
|
+
// method: "POST",
|
|
7
|
+
|
|
8
|
+
// headers: {
|
|
9
|
+
// "Content-Type": "application/json",
|
|
10
|
+
// "x-api-key": apiKey
|
|
11
|
+
// },
|
|
12
|
+
|
|
13
|
+
// body: JSON.stringify(payload)
|
|
14
|
+
// });
|
|
15
|
+
// } catch (err) {
|
|
16
|
+
// // Never crash client app
|
|
17
|
+
// console.error("SDK Send Failed:", err.message);
|
|
18
|
+
// }
|
|
19
|
+
// }
|
|
20
|
+
|
|
21
|
+
|
|
1
22
|
import fetch from "node-fetch";
|
|
2
23
|
|
|
3
24
|
/**
|
|
@@ -10,7 +31,8 @@ export async function sendError(apiKey, endpoint, payload) {
|
|
|
10
31
|
|
|
11
32
|
headers: {
|
|
12
33
|
"Content-Type": "application/json",
|
|
13
|
-
Authorization: apiKey
|
|
34
|
+
"Authorization": apiKey,
|
|
35
|
+
"x-api-key": apiKey
|
|
14
36
|
},
|
|
15
37
|
|
|
16
38
|
body: JSON.stringify(payload)
|
package/src/handlers.js
CHANGED
|
@@ -1,14 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
process.on("uncaughtException", (err) => {
|
|
8
|
-
capture(err);
|
|
1
|
+
import { sendError } from "./client.js";
|
|
2
|
+
|
|
3
|
+
export function attachHandlers(config) {
|
|
4
|
+
// Promise errors
|
|
5
|
+
process.on("unhandledRejection", err => {
|
|
6
|
+
report(err, config);
|
|
9
7
|
});
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
// Crash errors
|
|
10
|
+
process.on("uncaughtException", err => {
|
|
11
|
+
report(err, config);
|
|
13
12
|
});
|
|
14
|
-
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function report(err, config) {
|
|
16
|
+
const payload = {
|
|
17
|
+
message: err.message,
|
|
18
|
+
stack: err.stack,
|
|
19
|
+
type: "system",
|
|
20
|
+
|
|
21
|
+
service: config.service,
|
|
22
|
+
env: config.env,
|
|
23
|
+
|
|
24
|
+
timestamp: new Date()
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
sendError(
|
|
28
|
+
config.apiKey,
|
|
29
|
+
config.endpoint,
|
|
30
|
+
payload
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// import { capture } from "./index.js";
|
|
36
|
+
|
|
37
|
+
// /**
|
|
38
|
+
// * Attach process-level handlers
|
|
39
|
+
// */
|
|
40
|
+
// export function attachHandlers() {
|
|
41
|
+
// process.on("uncaughtException", (err) => {
|
|
42
|
+
// capture(err);
|
|
43
|
+
// });
|
|
44
|
+
|
|
45
|
+
// process.on("unhandledRejection", (err) => {
|
|
46
|
+
// capture(err);
|
|
47
|
+
// });
|
|
48
|
+
// }
|
package/src/index.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import { expressMiddleware } from "./middleware.js";
|
|
2
|
-
import { sendError } from "./client.js";
|
|
3
2
|
import { attachHandlers } from "./handlers.js";
|
|
3
|
+
import { sendError } from "./client.js";
|
|
4
4
|
import { getMeta } from "./utils.js";
|
|
5
5
|
|
|
6
6
|
let config = null;
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Initialize SDK
|
|
10
|
-
*/
|
|
11
8
|
function init(options) {
|
|
12
|
-
if (!options
|
|
13
|
-
throw new Error("apiKey
|
|
9
|
+
if (!options.apiKey) {
|
|
10
|
+
throw new Error("apiKey required");
|
|
14
11
|
}
|
|
15
12
|
|
|
16
13
|
config = {
|
|
@@ -28,16 +25,12 @@ function init(options) {
|
|
|
28
25
|
attachHandlers(config);
|
|
29
26
|
}
|
|
30
27
|
|
|
31
|
-
/**
|
|
32
|
-
* Capture error manually
|
|
33
|
-
*/
|
|
34
28
|
async function capture(error, extra = {}) {
|
|
35
29
|
if (!config) return;
|
|
36
30
|
|
|
37
31
|
const payload = {
|
|
38
|
-
message: error
|
|
39
|
-
|
|
40
|
-
stack: error?.stack,
|
|
32
|
+
message: error.message || error,
|
|
33
|
+
stack: error.stack,
|
|
41
34
|
|
|
42
35
|
type: "manual",
|
|
43
36
|
|
|
@@ -49,7 +42,7 @@ async function capture(error, extra = {}) {
|
|
|
49
42
|
...extra
|
|
50
43
|
},
|
|
51
44
|
|
|
52
|
-
timestamp: new Date()
|
|
45
|
+
timestamp: new Date()
|
|
53
46
|
};
|
|
54
47
|
|
|
55
48
|
await sendError(
|
|
@@ -65,8 +58,84 @@ export {
|
|
|
65
58
|
expressMiddleware
|
|
66
59
|
};
|
|
67
60
|
|
|
61
|
+
|
|
68
62
|
export default {
|
|
69
63
|
init,
|
|
70
64
|
capture,
|
|
71
65
|
expressMiddleware
|
|
72
|
-
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
// import { expressMiddleware } from "./middleware.js";
|
|
71
|
+
// import { sendError } from "./client.js";
|
|
72
|
+
// import { attachHandlers } from "./handlers.js";
|
|
73
|
+
// import { getMeta } from "./utils.js";
|
|
74
|
+
|
|
75
|
+
// let config = null;
|
|
76
|
+
|
|
77
|
+
// /**
|
|
78
|
+
// * Initialize SDK
|
|
79
|
+
// */
|
|
80
|
+
// function init(options) {
|
|
81
|
+
// if (!options?.apiKey) {
|
|
82
|
+
// throw new Error("apiKey is required");
|
|
83
|
+
// }
|
|
84
|
+
|
|
85
|
+
// config = {
|
|
86
|
+
// apiKey: options.apiKey,
|
|
87
|
+
|
|
88
|
+
// endpoint:
|
|
89
|
+
// options.endpoint ||
|
|
90
|
+
// "https://concordiq.onrender.com/api/v1/ingest/events",
|
|
91
|
+
|
|
92
|
+
// env: options.env || "production",
|
|
93
|
+
|
|
94
|
+
// service: options.service || "app"
|
|
95
|
+
// };
|
|
96
|
+
|
|
97
|
+
// attachHandlers(config);
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
// /**
|
|
101
|
+
// * Capture error manually
|
|
102
|
+
// */
|
|
103
|
+
// async function capture(error, extra = {}) {
|
|
104
|
+
// if (!config) return;
|
|
105
|
+
|
|
106
|
+
// const payload = {
|
|
107
|
+
// message: error?.message || String(error),
|
|
108
|
+
|
|
109
|
+
// stack: error?.stack,
|
|
110
|
+
|
|
111
|
+
// type: "manual",
|
|
112
|
+
|
|
113
|
+
// env: config.env,
|
|
114
|
+
// service: config.service,
|
|
115
|
+
|
|
116
|
+
// meta: {
|
|
117
|
+
// ...getMeta(),
|
|
118
|
+
// ...extra
|
|
119
|
+
// },
|
|
120
|
+
|
|
121
|
+
// timestamp: new Date().toISOString()
|
|
122
|
+
// };
|
|
123
|
+
|
|
124
|
+
// await sendError(
|
|
125
|
+
// config.apiKey,
|
|
126
|
+
// config.endpoint,
|
|
127
|
+
// payload
|
|
128
|
+
// );
|
|
129
|
+
// }
|
|
130
|
+
|
|
131
|
+
// export {
|
|
132
|
+
// init,
|
|
133
|
+
// capture,
|
|
134
|
+
// expressMiddleware
|
|
135
|
+
// };
|
|
136
|
+
|
|
137
|
+
// export default {
|
|
138
|
+
// init,
|
|
139
|
+
// capture,
|
|
140
|
+
// expressMiddleware
|
|
141
|
+
// };
|