Catch Error if you can ๐Ÿ˜Ž [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—๋Ÿฌ ์ฒ˜๋ฆฌ]

Sanghoon Hanยท2022๋…„ 9์›” 10์ผ
0
post-thumbnail

์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ api ํ†ต์‹ ์„ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜์Šต๋‹ˆ๋‹ค.
์ด๋กœ ์ธํ•ด ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ž‘์„ฑํ•˜๋‹ค ๋ณด๋‹ˆ ๋ฌธ๋“ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜๋ฌธ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

์—๋Ÿฌ๊ฐ€ ๋งŒ์•ฝ ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜์—์„œ ์ผ์–ด๋‚˜๊ณ  ์™ธ๋ถ€ ํ•จ์ˆ˜์—์„œ try catch ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด๋„ ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ์„๊นŒ?
์—๋Ÿฌ๋ž€ ๋ฌด์—‡์ด์ง€?...์—.....๋Ÿฌ!? ๐Ÿ˜‡ ์—๋Ÿฌ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด ๋ด์•ผ๊ฒ ๊ตฐ..!

์—๋Ÿฌ์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ๊ฒฐ์‹ฌํ•˜๊ณ  ์ด๋ฒˆ ๊ธฐํšŒ์— ์ •๋ฆฌํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๋ฆฌ๋œ ๋‚ด์šฉ์„
์ € ์ฒ˜๋Ÿผ ์—๋Ÿฌ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•ด ์–ด๋ ค์›Œ ํ•˜์‹  ๋ถ„๋“ค์„ ์œ„ํ•ด ๊ธ€์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ธ€์—์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์—๋Ÿฌ๋ž€ ๋ฌด์—‡์ผ๊นŒ?
  • ์—๋Ÿฌ์˜ ์ข…๋ฅ˜,
  • ์ค‘์ฒฉ๋œ ์—๋Ÿฌ ํ•ธ๋“ค๋ง,
  • ๋™๊ธฐ์ ์ธ ์ฝ”๋“œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ์™€ ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ์žก๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ฐจ์ด

TLDR

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์‹œ์ ์„ call stack์— ๊ทธ๋ฆฌ๋ฉด ํ•ด๊ฒฐ๋ฒ•์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. ์—๋Ÿฌ(error) who are you?

๋จผ์ € ์—๋Ÿฌ(error)๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

์—๋Ÿฌ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์‹œ ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•ด ํ”„๋กœ๊ทธ๋žจ์— ๋” ํฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๋ฐœ์ƒ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” 2๊ฐ€์ง€ ์ƒํ™ฉ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋˜์ ธ์ง‘๋‹ˆ๋‹ค.

  1. ์ฒซ ๋ฒˆ์งธ๋กœ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋Ÿฐํƒ€์ž„ ์‹œ์— ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค
  2. ๋‘ ๋ฒˆ์งธ๋กœ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ •์˜ํ•œ ์˜ˆ์™ธ ์ƒํ™ฉ์œผ๋กœ ์ธํ•ด ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘๋ฒˆ์žฌ ๋ฐฉ๋ฒ•์ธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์—๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

new Error(message, option)

์œ„์˜ ๊ธฐ๋ณธ error constructor๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ constructors๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

2. Error์˜ constructors์ข…๋ฅ˜

1.EvalError

  • evalํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ ์ธ์Šคํ„ด์Šค ์ž…๋‹ˆ๋‹ค.

2. RangeError

  • ์ˆซ์ž ๋ณ€์ˆ˜๋‚˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋„˜์—ˆ์„๋Œ€ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์• ๋Ÿฌ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.

3. ReferenceError

  • ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋‚˜ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ–ˆ์„๋•Œ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์—๋Ÿฌ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.

4. SyntaxError

  • ๋ฌธ๋ฒ•์ ์ธ ์—๋Ÿฌ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ์œ„ํ•ด ์กด์žฌํ•˜๋Š” ์—๋Ÿฌ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.

5.TypeError

  • ์œ ํšจํ•˜์ง€ ์•Š์€ ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ํ‘œํ˜„ํ• ๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.

6.URIError

  • encodeURI ํ˜น์€ decodeURI์— ์œ ํšจํ•˜์ง€ ์•Š์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ–ˆ์Œ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค.

์›ํ•˜๋Š” custom ์—๋Ÿฌ๋ฅผ ๋งŒ๋“œ๋Š” ์ž์„ธํ•œ ๋ฐฉ๋ฒ•์€ ๋งํฌ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”!

3. ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ [๋™๊ธฐ์‹]

๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด main.js ์•ˆ์— foo ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  foo ํ•จ์ˆ˜ ์•ˆ์—์„œ fee ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ fee์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ foo ๋Œ€์‹ 
foo๋ฅผ ํ˜ธ์ถœํ•œ main.js์—์„œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

//main.js
function foo() {
  function fee() {
    throw new Error("error is made in fee");
  }
  fee();
}

try {
  foo();
} catch (e) {
  console.log(e.message);
}

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด

error is made in fee

error๋ฅผ main.js์˜ catch ๋ฌธ์—์„œ ์—๋Ÿฌ๋ฅผ ํ•ธ๋“ค๋ง ํ•œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์œ„ ๊ณผ์ •์„ ํ•จ์ˆ˜ call stack์œผ๋กœ ๋„์‹ํ™”๋ฅผ ํ•ด๋ณด๋ฉด ๋‹ค์Œ ์•„๋ž˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

(1) ์œ„์ฒ˜๋Ÿผ try block ์•ˆ์—์„œ foo๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

(2) ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ foo ์•ˆ์—์„œ ํ•จ์ˆ˜ fee๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

(3) ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ fee ์—์„œ throw new Error("error is made in fee"); ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ try๋กœ ๊ฐ์‹ธ์ง„ call stack ๋‚ด์—์„œ ์‹คํ–‰๋˜์—ˆ๊ธฐ์— ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(4) error๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” catch ๋ฌธ์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜์—์„œ ์‹คํ–‰๋˜์—ˆ์ง€๋งŒ.
์‚ฌ์‹ค์€ ๊ฐ™์€ ํ•˜๋‚˜์˜ call stack์—์„œ ์Œ“์—ฌ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
์ด๋กœ ์ธํ•ด ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜์—์„œ ๋ฐœ์ƒํ•œ error์˜ ๊ฒฝ์šฐ ๋ฐ–์—์„œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ๋„
์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ ์ค‘์š”ํ•œ ์‚ฌ์‹ค์€ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜๋Š” ํ•จ์ˆ˜์˜ ์œ„์น˜๊ฐ€ ์ค‘์š”ํ•œ ๊ฒŒ ์•„๋‹ˆ๋ผ
์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์‹œ์ ์˜ call stack์ด ์ค‘์š”ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋งŒ์•ฝ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

4. ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ๋™๊ธฐ์‹ ํ˜ธ์ถœ์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด?

function foo() {
  function feePromise() {
    return Promise.reject(new Error("error made in feePromise"));
  }
  feePromise();
}

try {
  foo()
} catch (e) {
  console.log(e.message)
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด main.js์•ˆ์— fooํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
๋™๊ธฐ์‹์œผ๋กœ ์‹คํ–‰๋˜๋Š” fooํ•จ์ˆ˜
๊ทธ ์•ˆ์— Promise๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” feePromiseํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

main.js์—์„œ ์œ„์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜ foo๋ฅผ try๋ฌธ์—์„œ ํ˜ธ์ถœํ•˜๊ณ  ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

Uncaught (in promise) Error: error made in feePromise

์œ„์ฒ˜๋Ÿผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ try catch๋ฌธ์œผ๋กœ ์žก์•„๋‚ด์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.
์™œ ์žก์•„๋‚ด์ง€ ๋ชปํ–ˆ์„๊นŒ์š”?
์žก์•„๋‚ด์ง€ ๋ชปํ•œ ์ด์œ ๋ฅผ call stack ๊ด€์ ์—์„œ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜ ๊ทธ๋ฆผ์„ ํ™•์ธํ•ด ๋ณด์„ธ์š”

(1) ์ฒซ ๋ฒˆ์งธ๋กœ ๋™๊ธฐ์ ์ธ ํ•จ์ˆ˜ foo๊ฐ€ ํ˜ธ์ถœ์ด ๋ฉ๋‹ˆ๋‹ค. [๋™๊ธฐ ์‹œ์ž‘]
(2) feePromise๊ฐ€ try ๋ฌธ์œผ๋กœ ๊ฐ์‹ธ์ง„ call stack ์•ˆ์—์„œ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.
(3) Promise.reject๊ฐ€ ๋ฐ˜ํ™˜์ด ๋ฉ๋‹ˆ๋‹ค. [๋น„๋™๊ธฐ]
(4) ์ƒํƒœ๊ฐ€ rejected์ธ ๋ฐ˜ํ™˜๋œ Promise๊ฐ€ microTaskQueue์— ์ถ”๊ฐ€๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. [๋น„๋™๊ธฐ]
(5) try ๋ฌธ์œผ๋กœ ์‹คํ–‰๋œ call stack์ด ๋ชจ๋‘ ํ˜ธ์ถœ๋˜์–ด ์ข…๋ฃŒ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. [๋™๊ธฐ ์ข…๋ฃŒ]
(6) ๋” ์ด์ƒ ์‹คํ–‰์‹œํ‚ฌ call stack์ด ์—†๊ธฐ์— microTaskQueue์— ์žˆ๋˜ Promise๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
(7) Error๊ฐ€ ๋ฐœ์ƒ๋˜์—ˆ์ง€๋งŒ try catch ๋ธ”๋ก ๋‚ด์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š์•˜๊ธฐ์— ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

์œ„์—์„œ ๋ฌธ์ œ๊ฐ€ ๋œ ๋ถ€๋ถ„์€ ๋ฐ”๋กœ 5๋ฒˆ: try๋ฌธ์œผ๋กœ ์‹คํ–‰๋œ call stack์ด ๋ชจ๋‘ ํ˜ธ์ถœ๋˜์–ด ์ข…๋ฃŒ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ณผ [๋™๊ธฐ ์ข…๋ฃŒ]
7๋ฒˆ: Error๊ฐ€ ๋ฐœ์ƒ๋˜์—ˆ์ง€๋งŒ try catch ๋ธ”๋ก ๋‚ด์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š์•˜๊ธฐ์— ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. [๋น„๋™๊ธฐ ์ฝ”๋“œ ์‹คํ–‰]
๋ถ€๋ถ„ ์ž…๋‹ˆ๋‹ค.

์ฆ‰ ๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ try catch๋Š” ๋น„๋™๊ธฐ ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋˜๊ณ  ์ข…๋ฃŒ๋˜๊ธฐ์— ๋น„๋™๊ธฐ ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋Š” ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

5. ๋น„๋™๊ธฐ ์—๋Ÿฌ ์ฒ˜๋ฆฌ

์ง€๊ธˆ๊นŒ์ง€ call stack๊ด€์ ์—์„œ ๋ณธ๋‹ค๋ฉด
๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์—๋Ÿฌ๋Š” ๋น„๋™๊ธฐ ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” call stack์—์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด๋ฉ๋‹ˆ๋‹ค.
๋น„๋™๊ธฐ์ ์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. Promise๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ .catch๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  2. async ํ•จ์ˆ˜ ์•ˆ์—์„œ Promise๋ฅผ ๋ฐ˜ํ™˜๋˜๋Š” ํ•จ์ˆ˜๋ฅผ await์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์ด๋ฅผ try catch๋กœ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.

1. Promise๋ฐ˜ํ™˜ ํ•จ์ˆ˜ ํ˜ธ์ถœํ•  ๋•Œ .catch๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ

function foo() {
  function feePromise() {
    return Promise.reject(new Error("error made in feePromise"));
  }
  feePromise()
    .then(res => console.log(res))
    .catch(err => console.log(err.message));
}

foo()
console.log('๋™๊ธฐ์‹ ์ฝ”๋“œ is done')

์œ„์ฒ˜๋Ÿผ feePromise()๋ฅผ ํ˜ธ์ถœํ•œ ๋ถ€๋ถ„์— .catch๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์œ„๋ฅผ call stack ๊ด€์ ์œผ๋กœ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

(1) foo๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
(2) feePromise๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
(3) Promise๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ . microTaskQueue์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.
์ด๋•Œ Promise์—๋Š” ํ›„์— error๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” catch ๋ฌธ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
(4) foo ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๊ณ  ๋™๊ธฐ์‹ ์ฝ”๋“œ ์ข…๋ฃŒ๋ฅผ ์•Œ๋ฆฌ๋Š” console.log๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

(5) call stack์ด ๋น„์–ด์žˆ๊ธฐ์— microTaskQueue์— ์žˆ๋Š” Promise๊ฐ€ call stack์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
(6) call stack์—์„œ then์—์„œ ์‹คํ–‰๋œ (res) => console.log(res)๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „ Error๊ฐ€ ๋‹ด๊ธด Promise๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
(7) ์ด๋•Œ ์ž‘์„ฑํ–ˆ๋˜ .catch๋กœ ๋ฐœ์ƒ๋œ ์—๋Ÿฌ๋ฅผ ์žก์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  microTaskQueue์— ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
(8) call stack์ด ๋น„์–ด์žˆ๊ธฐ์— ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” (err) => console.log(err.message)๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
(9) ์ฝ˜์†”์ฐฝ์— error is made in fee๊ฐ€ ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค.

2. async ํ•จ์ˆ˜ ๋‚ด์—์„œ try catch ์ฒ˜๋ฆฌํ•˜๊ธฐ


async function foo() {
  function feePromise() {
    return Promise.reject(new Error("error made in feePromise"));
  }
  try {
     await feePromise()
  } catch(e) {
   	console.log(e.message) 
  }
}

foo()
console.log('๋™๊ธฐ์‹ ์ฝ”๋“œ is done')

foo๋ฅผ async ํ•จ์ˆ˜๋กœ ๋ฐ”๊พธ๊ณ  feePromise๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ถ„์„ await์œผ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ๋˜๋ฉด feePromise๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.
๋น„๋™๊ธฐ ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ถ€๋ถ„์„ try catch ๋‚ด์—์„œ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ
์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฅผ ํ•œ๋ฒˆ call stack ๊ด€์ ์—์„œ ๋„์‹ํ™” ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

(1) foo๋ฅผ async ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ํ˜ธ์ถœ์„ ํ•ฉ๋‹ˆ๋‹ค.
(2) await์œผ๋กœ Error๋ฅผ ๋‹ด์€ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” feePromise๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
(3) await์œผ๋กœ ํ˜ธ์ถœํ•˜์˜€๊ธฐ์— feePromise๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ์— foo๋Š” ๋น„๋™๊ธฐ์‹์œผ๋กœ ์ž‘๋™์„ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ microTaskQueue๋กœ foo๋Š” ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
(4) ์ดํ›„ console.log('๋™๊ธฐ์‹ ์ฝ”๋“œ is done') ์ด ์‹คํ–‰๋˜๋ฉด์„œ ๋™๊ธฐ์‹์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

(5) ๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ call stack์ด ๋งˆ๋ฌด๋ฆฌ๊ฐ€ ๋˜์–ด ๋น„์—ˆ๊ธฐ์— microTaskQueue์— ์žˆ๋˜ ํ•จ์ˆ˜ foo๊ฐ€ ๋‹ค์‹œ call stack์œผ๋กœ ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Error๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Promise๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
(6) ์ด๋•Œ try catch๋ฌธ์ด ๋ฐ˜ํ™˜๋œ Error๋ฅผ ์žก๊ณ  console.log(e.message)๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
(7) ์ด๋กœ ์ธํ•ด error is made in fee๊ฐ€ ์ฝ˜์†” ์ฐฝ์— ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
(8, 9) ์ดํ›„์— ํ•จ์ˆ˜ foo๊ฐ€ ๋งˆ๋ฌด๋ฆฌ ๋˜์–ด call stack์„ ๋น„์›Œ๋ƒ…๋‹ˆ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ๋น„๋™๊ธฐ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.
๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋Š” ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ฒซ ๋ฒˆ์งธ๋ก  Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ• ๋•Œ .catch๋ฅผ ํ™œ์šฉํ•ด์„œ ์—๋Ÿฌ๋ฅผ ์žก์•„๋ƒ…๋‹ˆ๋‹ค.
๋‘ ๋ฒˆ์งธ๋ก  Promise๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ async๋กœ ํ•จ์ˆ˜๋กœ ์„ ์–ธํ•˜๊ณ  Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ await์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  try catch ๋ฌธ์„ ํ™œ์šฉํ•˜๋ฉด ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

6. ๋งˆ๋ฌด๋ฆฌ (call stack์„ ๊ทธ๋ฆฌ์ž.)

์ด๋ฒˆ ์‹œ๊ฐ„์— ์—๋Ÿฌ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ  ๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์™€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ• ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์ด ์–ด๋Š ์œ„์น˜์—์„œ ์„ ์–ธ๋œ ๊ฒƒ๋ณด๋‹จ ์—๋Ÿฌ๊ฐ€ call stack ๊ด€์ ์—์„œ ์–ด๋Š ์‹œ์ ์— ๋ฐœ์ƒํ•˜๋Š”์ง€๊ฐ€ ์ค‘์š”ํ•จ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

call stack๊ด€์ ์œผ๋กœ ๋ถ„์„ํ•˜๋ฉด ๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์™€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์„ ์‰ฝ๊ฒŒ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ƒํ™ฉ์ด ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ˜ธ์ถœ call stack์„ ๊ทธ๋ ค์„œ ๋ถ„์„ํ•ด ๋ณธ๋‹ค๋ฉด ๊นŒ๋‹ค๋กญ๊ธฐ๋งŒ ํ–ˆ๋˜ ์—๋Ÿฌ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์ง€์ผœ์ฃผ๋Š” ์ข‹์€ ์ˆ˜ํ˜ธ์ž๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ๊ธด ๊ธ€ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

7. references

error์— ๋Œ€ํ•œ ์ •๋ณด
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

javascript callstack ์‹œ๊ฐํ™”
https://dev.to/lydiahallie/javascript-visualized-promises-async-await-5gke

error ์ฒ˜๋ฆฌ ๊ด€๋ จ ์ข‹์€๊ธ€
https://kinsta.com/blog/errors-in-javascript/

profile
Fail Fast learn Faster

0๊ฐœ์˜ ๋Œ“๊ธ€