亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

? ? ????? JS ???? Node.js?? ???? ???? ?? ?? ??? ??

Node.js?? ???? ???? ?? ?? ??? ??

Nov 30, 2024 pm 03:29 PM

Building a Simple and Effective Error-Handling System in Node.js

? ??????? ??? ? ?? ???? ????? ????. ???? ??? ??? ?????, ???? ?????, ??? ?? ??? ??? ? ????. ???? ??????? ??? ?? ??? ??? ????? ??? ??? ???? ???? ?? ??????.

? ?????? Node.js ???????? ???? ???? ?? ?? ???? ???? ??? ?? ??????. ?? ?????! ?

?? ??? ??

JavaScript?? ??? ????? Error ???? ????? ?????. ??? ???? ??? "??"? ? ???, ??????? ?? ???? ?? ?? ??? ? ????. ??? ??? ????????? ?? Error ?????? ??? ???? ????? ? ????.

??? ??? ?? ?? ???? ?????!
??? ?? ?? ??? ??

??? ????? ?? ?? ??? ???? ? ??? ? ?? AppError?? ??????.

export class AppError extends Error {
    constructor(name, httpCode, description, isOperational, errors = []) {
        super(description); // Call the parent class (Error) constructor
        this.name = name; // Name of the error (e.g., ValidationError)
        this.httpCode = httpCode; // HTTP status code (e.g., 400, 404, 500)
        this.isOperational = isOperational; // Flag to distinguish between operational and system errors
        this.errors = errors; // Optional array of detailed error messages
    }
}

??? ??????

  • ??: ValidationError ?? NotFoundError? ?? ?? ??? ?????.
  • httpCode: ??? ??? ?? 400, ?? ??? ?? 500 ? ??? HTTP ?? ??? ?????.
  • ??: ??? ???? ??? ???? ???? ?????.
  • isOperational: ??? ??? ???(?: ??? ?? ??) ?? ??? ?? ??(?: ??)? ???? ?? ??????.
  • ??: ??? ?? ?? ???? ?? ??? ???? ??? ?????(?? ??? ???).

??? ?? ?? ????

?? ??? ?? ?? ???? ???? ??? ??? ??? ??? ??? ?????. ???? ??:

export const errorHandler = (err, req, res, next) => {
    if (err instanceof AppError) {
        // Handle operational errors
        res.status(err.httpCode).json({
            success: false,
            error: err.name,
            message: err.message,
            errors: err.errors || [],
        });
    } else {
        // Handle unexpected errors
        console.error('Unexpected Error:', err.stack); // Log the stack trace for debugging
        res.status(500).json({
            success: false,
            error: 'InternalServerError',
            message: 'Something went wrong. Please try again later.',
        });
    }
};

??? ??? ????
?? ??? ?????:
??? AppError? ????? ?? ?? ??? ?????. ???? ??? ??? ????(?: ??? ?? ?? ?? ??? ??).
AppError? ?? ?? ??? ?? ??? ?????. ?? ??? ????? ???? ?? ?? ? ????.

Responds with the right status code and message:
    For AppError instances, it sends a structured JSON response containing the error details.
    For unexpected errors, it sends a generic 500 response to the user and logs the details for debugging.

?? ???

Express ?? ???? ??? ????? ?? ??? ??? ??? ??? ??? ???. ??? ?? AppError ???? ??? ? ????.

app.post('/submit-form', (req, res, next) => {
    const { name, email } = req.body;

    if (!name || !email) {
        const errors = [];
        if (!name) errors.push({ field: 'name', message: 'Name is required.' });
        if (!email) errors.push({ field: 'email', message: 'Email is required.' });

        return next(new AppError('ValidationError', 400, 'Invalid input data.', true, errors));
    }

    res.json({ success: true, message: 'Form submitted successfully!' });
});

// Register the error-handling middleware
app.use(errorHandler);

??? ???? ??? ????
???? ???? ??? ?? AppError ????? ?? ??? ?????.
errorHandler ????? ?? ???? ???? JSON ??? ????.

{
    "success": false,
    "error": "ValidationError",
    "message": "Invalid input data.",
    "errors": [
        { "field": "name", "message": "Name is required." },
        { "field": "email", "message": "Email is required." }
    ]
}

? ??? ???? ??? ??????

  • ???: ??? ?? ?? ???? ???? ??? ? ??? ???? ?????? ? ????.
  • ???: ?? ??? ?? ??? ??? ???? ????? ???? ? ?? ??? ? ????.
  • ?? ??: ?? ???? ??? ??? ?? ????? ?? ??? ?????.

??
?? ??? ??? ??? ????! ??? ?? ?? ???? ????? ???? ? ???? ???? ??? ???? ??? ???? ??? ???? ???? ??? ? ????.

??? ?? ??? ?? ??? ??????? Node.js?? ??? ???? ?? ??? ??? ???? ?? ??? ?????!

? ??? Node.js?? ???? ???? ?? ?? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1597
29
PHP ????
1488
72
NYT ?? ??? ??
131
836
???
??? ??? JavaScript?? ??? ?????? ??? ??? JavaScript?? ??? ?????? Jul 04, 2025 am 12:42 AM

JavaScript? ??? ?? ????? ??? ?? ??? ??? ?? ?? ?? ????? ?? ???? ???? ?????. ??? ?? ???? ?? ??? ?? ??? ???? ???? ?? ?? ???? ???? ?????. ?? ??, ??? ? ?? ???? ??? (? : ??? null? ??) ?? ??? ????? ??????. ??? ??? ???? ??? ??? ????. closure?? ?? ??? ?? ??; ? ??? ??? ?? ?? ???? ?? ???? ????. V8 ??? ?? ???, ?? ??, ??/?? ???? ?? ??? ?? ??? ??? ????? ?? ??? ?? ??? ????. ?? ?? ???? ??? ??? ??? ??? ???? ????? ?? ?? ???? ?? ???????.

node.js?? HTTP ????? ??? node.js?? HTTP ????? ??? Jul 13, 2025 am 02:18 AM

Node.js?? HTTP ??? ???? ? ?? ???? ??? ????. 1. ?? ????? ????? ??? ??? ? ?? ????? ?? ?? ? https.get () ??? ?? ??? ??? ? ?? ????? ?? ??? ?????. 2.axios? ??? ???? ? ?? ??????. ??? ??? ??? ??? ??? ??? ???/???, ?? JSON ??, ???? ?? ?????. ??? ?? ??? ????? ?? ????. 3. ?? ??? ??? ??? ??? ???? ???? ??? ??? ???? ?????.

JavaScript ??? ?? : ?? ? ?? JavaScript ??? ?? : ?? ? ?? Jul 13, 2025 am 02:43 AM

JavaScript ??? ??? ?? ?? ? ?? ???? ????. ?? ???? ???, ??, ??, ?, ???? ?? ? ??? ?????. ?? ????? ?? ?? ? ? ??? ????? ?? ??? ??? ????. ??, ?? ? ??? ?? ?? ??? ??? ??? ???? ??? ??? ???? ??? ?? ??? ????. ?? ? ????? ??? ???? ? ??? ? ??? TypeofNull? ??? ?????? ??? ? ????. ? ? ?? ??? ???? ?????? ????? ???? ??? ???? ? ??? ? ? ????.

JavaScript Time Object, ??? Google Chrome? EACTEXE, ? ?? ? ???? ?????. JavaScript Time Object, ??? Google Chrome? EACTEXE, ? ?? ? ???? ?????. Jul 08, 2025 pm 02:27 PM

?????, JavaScript ???! ?? ? JavaScript ??? ?? ?? ?????! ?? ?? ??? ??? ??? ? ????. Deno?? Oracle? ?? ??, ??? JavaScript ?? ??? ????, Google Chrome ???? ? ??? ??? ???? ?????. ?????! Deno Oracle? "JavaScript"??? ????? Oracle? ?? ??? ??? ??????. Node.js? Deno? ??? ? Ryan Dahl? ??? ?????? ???? ????? JavaScript? ??? ???? Oracle? ????? ???? ?????.

REACT vs Angular vs Vue : ?? JS ??? ??? ?? ????? REACT vs Angular vs Vue : ?? JS ??? ??? ?? ????? Jul 05, 2025 am 02:24 AM

?? JavaScript ??? ??? ??? ?????? ?? ??? ?? ?? ??? ?? ???? ????. 1. ??? ???? ???? ?? ??? ?? ? ? ???? ??? ??? ?? ? ?? ????? ?????. 2. Angular? ?????? ??? ?? ???? ? ?? ?? ??? ??? ??? ???? ?????. 3. VUE? ???? ?? ??? ???? ?? ?? ??? ?????. ?? ?? ?? ??, ? ??, ???? ???? ? SSR? ???? ??? ??? ??? ???? ? ??? ?????. ???, ??? ??? ??? ????? ????. ??? ??? ??? ??? ?? ????.

JavaScript?? ?? ?? ??? (IIFE)? ????? JavaScript?? ?? ?? ??? (IIFE)? ????? Jul 04, 2025 am 02:42 AM

iife (?? invokedfunctionexpression)? ?? ??? ???? ?? ????? ??? ???? ?? ??? ????? ?? ??? ? ?????. ??? ?? ?? ??? ???? ? ?? ??? ??? ?? (function () {/code/}) ();. ?? ???? ??? ?????. 1. ?? ??? ??? ?? ???? ?? ??? ??? ?????. 2. ?? ??? ??? ???? ?? ?? ??? ????. 3. ?? ?? ??? ????? ?? ???? ???????? ?? ? ??. ???? ?? ???? ?? ??? ES6 ??? ??? ??? ?? ? ??? ????? ??? ? ???? ???????.

?? ??? : JavaScript? ??, ?? ?? ? ?? ????? ?? ??? : JavaScript? ??, ?? ?? ? ?? ????? Jul 08, 2025 am 02:40 AM

??? JavaScript?? ??? ??? ?????? ?? ???????. ?? ??, ?? ?? ? ??? ??? ?? ????? ????? ?????. 1. ?? ??? ??? ????? ???? ??. ()? ?? ??? ??? ?????. ?. ()? ?? ??? ?? ??? ??? ?? ? ? ????. 2. ?? ??? .catch ()? ???? ?? ??? ??? ?? ??? ??????, ??? ???? ???? ????? ??? ? ????. 3. Promise.all ()? ?? ????? (?? ?? ?? ? ??????? ??), Promise.Race () (? ?? ??? ?? ?) ? Promise.AllSettled () (?? ??? ???? ??)

?? API? ???? ??? ???? ??? ?????? ?? API? ???? ??? ???? ??? ?????? Jul 08, 2025 am 02:43 AM

Cacheapi? ?????? ?? ???? ??? ???? ???, ?? ??? ??? ?? ???? ? ??? ?? ? ???? ??? ??????. 1. ???? ????, ??? ??, ?? ?? ?? ???? ???? ??? ? ????. 2. ??? ?? ?? ??? ?? ? ? ????. 3. ?? ?? ?? ?? ?? ??? ??? ?? ?????. 4. ??? ???? ?? ?? ???? ?? ?? ?? ?? ?? ???? ?? ?? ??? ??? ? ????. 5. ?? ???? ??, ??? ??? ? ??? ??, ?? ??? ? ?? ???? ???? ???? ? ?? ?????. 6.?? ??? ?? ?? ?? ??, ???? ?? ? HTTP ?? ????? ?????? ???????.

See all articles