1.0.43 • Published 12 days ago

try-catch-cloud v1.0.43

Weekly downloads
-
License
ISC
Repository
-
Last release
12 days ago

Try Catch Cloud

This package helps with error tracking and monitoring for your Node.js applications with a minimum of effort.

Usage

Note: You can get API key on https://try-catch-cloud-fe.pages.dev/

import { ErrorUtility } from "try-catch-cloud";

// Change your-project-name to actual project name.
// Change your-api-key with API key from our website.
// Error logs will be associated with your project name and stored, referencing it.
const errorTrack = new ErrorUtility("your-project-name", "your-api-key");

try {
	...
} catch (error) {
    const context = { userId: '...', foo: 'bar' };
	await errorTrack.sendError(error, context);
}

The following approach lets you manually set all necessary request information regardless of what framework you use:

app.use((err, req, res, next) => {
    const { base64File, ...body } = req.body;
    errorTrack.sendErrorFromEndpoint(
        error,
        {
            url: req.originalUrl.split("?")[0],
            method: req.method,
            headers: req.headers,
            query: req.query,
            body,
        },
        { ...context }
    );
});

Middlewares

Express

Since Express 4.x.x (and older versions) does not pass errors occurring in async route handlers to error handlers, you must manually catch these errors using a try-catch block and pass them to the next function. Alternatively, you can use express-async-handler or a similar library to address this issue.

import { setupTryCatch, setContext } from 'try-catch-cloud/express';
import asyncHandler from 'express-async-handler';

// Create try-catch instance
const tryCatch = setupTryCatch({
    projectName: "your-project-name",
    apiKey: "your-api-key",
});

// Use `initialize` before other handlers
app.use(tryCatch.initialize);

app.get("/", (req, res) => {
    // Enhance error details with contextual information
    setContext(req, { message: req.query.message });

    ...

    res.json({ message: 'ok' });
});

app.get("/hello", asyncHandler(async (req, res) => {
    // Enhance error details with contextual information
    setContext(req, { message: req.query.message });

    ...

    res.json({ message: 'ok' });
}));

// Put `onError` after all controllers and before error handlers
app.use(tryCatch.onError);

// You can add custom error handler
app.use((error, req, res, next) => {
    res.status(500).json({ message: 'Internal server error' }).end();
});

Hono

import { tryCatch } from "try-catch-cloud/hono";

// Initialize tryCatch
app.use(
    tryCatch({
        projectName: "your-project-name",
        apiKey: "your-api-key",
    })
);

app.get("/:message", (c) => {
    // Enhance error details with contextual information
    c.get('tryCatch').setContext({ message: c.req.param('message') });

    ...

    return c.json({ message: 'ok' });
});

// Optionally add custom error handler
app.onError((e, c) => {
    c.get("tryCatch").setContext({
        userId: "...",
    });

    return c.json({ message: "Internal server error" }, 500);
});
1.0.43

12 days ago

1.0.42

1 month ago

1.0.41

1 month ago

1.0.40

2 months ago

1.0.39

2 months ago

1.0.38

3 months ago

1.0.37

3 months ago

1.0.36

3 months ago

1.0.35

3 months ago

1.0.33

3 months ago

1.0.34

3 months ago

1.0.32

3 months ago

1.0.29

3 months ago

1.0.31

3 months ago

1.0.30

3 months ago

1.0.28

3 months ago

1.0.26

3 months ago

1.0.25

3 months ago

1.0.24

3 months ago

1.0.27

3 months ago

1.0.19

4 months ago

1.0.22

4 months ago

1.0.21

4 months ago

1.0.20

4 months ago

1.0.23

4 months ago

1.0.18

4 months ago

1.0.17

4 months ago

1.0.16

4 months ago

1.0.15

4 months ago

1.0.14

4 months ago

1.0.13

4 months ago

1.0.11

4 months ago

1.0.10

4 months ago

1.0.12

4 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago