๐Ÿค“ try/catch ์—†์ด expressJs์—์„œ ๋น„๋™๊ธฐ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ!

9798 ๋‹จ์–ด webdevnodebeginnersjavascript
์ด ๋ธ”๋กœ๊ทธ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ›Œ๋ฅญํ•œ ๊ฐœ๋ฐœ ์ฝ˜ํ…์ธ ๋ฅผ ์œ„ํ•ด ๊ทธ๋ฅผ ๊ตฌ๋…ํ•˜์‹ญ์‹œ์˜ค.

๊ฒฝ๋กœ ์ฒ˜๋ฆฌ๊ธฐ ๋ฐ ๋ฏธ๋“ค์›จ์–ด ๋‚ด๋ถ€์˜ ๋™๊ธฐ ์ฝ”๋“œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋Š” ์ถ”๊ฐ€ ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋™๊ธฐ ์ฝ”๋“œ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด Express๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ํฌ์ฐฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋Š” ๋ชจ๋“  ๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ ์ž‘์—…์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ์ด์ „ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ express๋Š” ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๊ธฐ์™€ ํ•จ๊ป˜ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.status(200).send('Hello World!')
})

// Error handler for synchronous errors
// This should be the last middleware in the chain
app.use((err, req, res, next) => {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

app.listen(port, () => {
  console.log(`Example app 
  listening on port ${port}`)
})



๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋ถ€๋ถ„์˜ ๋ฐฑ์—”๋“œ๋Š” ์ผ๋ถ€ db ์ž‘์—…์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ํ•ญ์ƒ ๋น„๋™๊ธฐ์‹์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด์ „ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๊ธฐ ์ „์— ๋‹ค๋ฅธ ์ž‘์—…์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿคจ ๊ทธ๋ ‡๋‹ค๋ฉด ์ต์Šคํ”„๋ ˆ์Šค์—์„œ ๋น„๋™๊ธฐ ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
ํ•œ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•œ ํ•ด๊ฒฐ์ฑ…์€ try/catch๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  catch ๋ธ”๋ก์—์„œ next()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

next()๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ์šฐ๋ฆฌ๋Š” ์ต์Šคํ”„๋ ˆ์Šค๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ํฌ์ฐฉํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์‘๋‹ตํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

์ˆ˜์ •๋œ ๋ฒ„์ „์˜ ๊ฒฝ๋กœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

app.get('/', (req, res, next) => {
  try {
    // some db asyncrhonous operations
    res.status(200).send('Hello World!')
  }
  catch(error) {
    next(error)
  }
})



next(์˜ค๋ฅ˜)๋Š” ์šฐ๋ฆฌ์˜ ๊ธฐ๋ณธ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ˜ธ์ถœํ•  ๊ฒƒ์ด๊ณ  ์šฐ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋Š” ์—‰๋ง์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค! ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ.

๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ ์•ฑ์€ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ๋‹จ์ง€ ์„ฑ์žฅํ•  ๊ฒƒ์ด๊ณ  ์šฐ๋ฆฌ๋Š” ๋ฏธ๋ž˜์— ์šฐ๋ฆฌ๋ฅผ ๋†€๋ผ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค ์˜ค๋ฅ˜๋„ ๋†“์น˜๊ณ  ์‹ถ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

We will make something interesting without try/catch.



๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ๋น„๋™๊ธฐ(์ด์ƒ์ ์œผ๋กœ๋Š” ์•ฝ์†)๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฑฐ๋ถ€ ์‹œ next()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” promiseHandler ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•œ ์ค„์—

const promiseHandler = () =>  (req, res, next) => {
    Promise.resolve(fn(req, res, next)).catch(next)
  }

// and route becomes
app.get('/', promiseHandler (async (req, res, next) => {
    // some db asyncrhonous operations
    res.status(200).send('Hello World!')
}))



์ด ํ”„๋กœ๋ฏธ์Šค ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์„ฑ๊ณตํ•˜๋ฉด ํ•ด๊ฒฐํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ๋‹ค์Œ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ถ€ ์˜ค๋ฅ˜๋ฅผ ํ•ดํ‚นํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ๋‹ค์Œ ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. nodejs-error-handler

๋‚ด TypeScript ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•ด Promise ํ•ธ๋“ค๋Ÿฌ ๊ธฐ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import { NextFunction, Request, Response } from "express";

type fnType = (req : Request, res : Response, next : NextFunction) => void;

const promiseHandler = (fn: fnType) => 
  (req : Request, res : Response, next : NextFunction) => {
  Promise.resolve(fn(req  , res , next)).catch(next)
}

export default promiseHandler




nodejs์— ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ์•Œ๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ๋„๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ๋‚ด๊ฐ€ ๊ฐ๋„์—์„œ ๋งŒ๋“  ์‹ค์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ•ธ ๋‹น์‹ ์€ ์‹ ์ง„ ์›น ๊ฐœ๋ฐœ์ž์ด๋ฉฐ ๋ฐฉ๋ฌธ์—์„œ ๋ฐฐ์šธ ๋ฉ‹์ง„ CSS ์›น์‚ฌ์ดํŠธ๊ฐ€ ํ•„์š”ํ•˜์‹ญ๋‹ˆ๊นŒ?

์ง€์‹์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ด๋Ÿฌํ•œ ์œ ํ˜•์˜ ์ž‘์€ ๊ธฐ์‚ฌ๊ฐ€ ๋งˆ์Œ์— ๋“ค๋ฉด dev.to๋ฅผ ํŒ”๋กœ์šฐํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋” ๋งŽ์ด ์ž‘์„ฑํ•˜๊ณ  ์˜คํ”ˆ ์†Œ์Šค์— ๊ธฐ์—ฌํ•˜๋„๋ก ๋™๊ธฐ๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ•Š ํ‰ํ™”!

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ