@concordiq/error-sdk 1.0.0

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 ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@concordiq/error-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Error monitoring SDK",
5
+ "main": "src/index.js",
6
+ "type": "module",
7
+ "keywords": ["errors", "monitoring", "logging"],
8
+ "author": "Arowolo",
9
+ "license": "MIT"
10
+ }
package/readMe.md ADDED
@@ -0,0 +1,65 @@
1
+ npm install @concordiq/error-sdk
2
+
3
+
4
+ `Controller`
5
+ import ErrorSDK from "@arosebine/error-sdk";
6
+
7
+ ErrorSDK.init({
8
+ Authorization: "Bearer coiq-6ddfeede1d57a268f7131ef0597624200a8a02b7",
9
+
10
+ endpoint: "https://concordiq.onrender.com/api/v1/ingest/events",
11
+
12
+ env: "production",
13
+
14
+ service: "payment-api"
15
+ });
16
+
17
+
18
+ `
19
+ try {
20
+ processPayment();
21
+ } catch (err) {
22
+ ErrorSDK.capture(err, {
23
+ message: err.message,
24
+ stack: err.stack,
25
+ type: "crash"
26
+ });
27
+ }
28
+ `
29
+
30
+
31
+ `Express.Js `
32
+ export function expressHandler() {
33
+ return (err, req, res, next) => {
34
+ import("./index.js").then(sdk => {
35
+ sdk.default.capture(err, {
36
+ url: req.originalUrl,
37
+ method: req.method,
38
+ user: req.user?.id
39
+ });
40
+ });
41
+
42
+ next(err);
43
+ };
44
+ }
45
+
46
+ app.use(ErrorSDK.expressMiddleware());
47
+
48
+
49
+
50
+ `To publish it`
51
+ npm login
52
+ npm publish --access public
53
+
54
+
55
+
56
+ `To Test it Locally`
57
+ import ErrorSDK from "./src/index.js";
58
+
59
+ ErrorSDK.init({
60
+ apiKey: "test123",
61
+ endpoint: "http://localhost:4000/ingest"
62
+ });
63
+
64
+ throw new Error("Test Crash");
65
+
package/src/client.js ADDED
@@ -0,0 +1,19 @@
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
+ }
@@ -0,0 +1,32 @@
1
+ import { sendError } from "./client.js";
2
+
3
+ export function attachHandlers(config) {
4
+ // Promise errors
5
+ process.on("unhandledRejection", err => {
6
+ report(err, config);
7
+ });
8
+
9
+ // Crash errors
10
+ process.on("uncaughtException", err => {
11
+ report(err, config);
12
+ });
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
+ }
package/src/index.js ADDED
@@ -0,0 +1,57 @@
1
+ import { attachHandlers } from "./handlers.js";
2
+ import { sendError } from "./client.js";
3
+ import { getMeta } from "./utils.js";
4
+
5
+ let config = null;
6
+
7
+ function init(options) {
8
+ if (!options.apiKey) {
9
+ throw new Error("apiKey required");
10
+ }
11
+
12
+ config = {
13
+ apiKey: options.apiKey,
14
+
15
+ endpoint:
16
+ options.endpoint ||
17
+ "https://api.yoursaas.com/ingest",
18
+
19
+ env: options.env || "production",
20
+
21
+ service: options.service || "app"
22
+ };
23
+
24
+ attachHandlers(config);
25
+ }
26
+
27
+ async function capture(error, extra = {}) {
28
+ if (!config) return;
29
+
30
+ const payload = {
31
+ message: error.message || error,
32
+ stack: error.stack,
33
+
34
+ type: "manual",
35
+
36
+ env: config.env,
37
+ service: config.service,
38
+
39
+ meta: {
40
+ ...getMeta(),
41
+ ...extra
42
+ },
43
+
44
+ timestamp: new Date()
45
+ };
46
+
47
+ await sendError(
48
+ config.apiKey,
49
+ config.endpoint,
50
+ payload
51
+ );
52
+ }
53
+
54
+ export default {
55
+ init,
56
+ capture
57
+ };
package/src/utils.js ADDED
@@ -0,0 +1,9 @@
1
+ import os from "os";
2
+
3
+ export function getMeta() {
4
+ return {
5
+ host: os.hostname(),
6
+ platform: process.platform,
7
+ memory: process.memoryUsage().rss
8
+ };
9
+ }