[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-js2go-ep28-error-stacktrace-logging-all--*":3,"academy-blog-translations-blwvy96xwsavt05":80},{"data":4,"page":79,"perPage":79,"totalItems":79,"totalPages":79},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":73,"keywords":74,"locale":49,"published_at":75,"scheduled_at":13,"school_blog":71,"short_description":76,"slug":77,"status":69,"title":6,"updated":78,"updated_by":13,"views":72},"JS2GO EP.28 Advanced Error Handling: Stack Trace and Logging","sclblg987654321","school_blog_translations","\u003Cp>Error handling is a crucial part of professional software development, especially in large-scale systems or production environments where you need to debug and analyze issues efficiently. In this article, we explore advanced error handling in \u003Cstrong>JavaScript\u003C\u002Fstrong> and \u003Cstrong>Go\u003C\u002Fstrong>, including the use of \u003Cstrong>stack traces\u003C\u002Fstrong> and \u003Cstrong>logging\u003C\u002Fstrong> to analyze and resolve errors effectively.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. Errors and Stack Trace in JavaScript\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Creating and Handling Errors\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function divide(a, b) {\n  if (b === 0) {\n    throw new Error(\"Cannot divide by zero\");\n  }\n  return a \u002F b;\n}\n\ntry {\n  const result = divide(10, 0);\n} catch (err) {\n  console.error(\"Error:\", err.message);\n  console.error(err.stack); \u002F\u002F Display stack trace\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Advantages:\u003C\u002Fstrong>\u003Cbr>✔️ Stack trace shows exactly where the error occurred\u003Cbr>✔️ Error objects can be extended with custom properties\u003Cbr>✔️ Easy to use for debugging\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Limitations:\u003C\u002Fstrong>\u003Cbr>⚠️ Runtime errors must be handled with \u003Ccode inline=\"\">try\u002Fcatch\u003C\u002Fcode>\u003Cbr>⚠️ Asynchronous code requires \u003Ccode inline=\"\">try\u002Fcatch + async\u002Fawait\u003C\u002Fcode> or \u003Ccode inline=\"\">.catch()\u003C\u002Fcode>\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Handling Asynchronous Errors\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">async function fetchData() {\n  throw new Error(\"Network error\");\n}\n\nfetchData()\n  .then(data =&gt; console.log(data))\n  .catch(err =&gt; console.error(err.stack));\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. Errors and Stack Trace in Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Using the \u003Ccode inline=\"\">error\u003C\u002Fcode> Type\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n)\n\nfunc divide(a, b int) (int, error) {\n    if b == 0 {\n        return 0, errors.New(\"cannot divide by zero\")\n    }\n    return a \u002F b, nil\n}\n\nfunc main() {\n    result, err := divide(10, 0)\n    if err != nil {\n        fmt.Println(\"Error:\", err)\n    } else {\n        fmt.Println(\"Result:\", result)\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Creating a Stack Trace\u003C\u002Fh3>\u003Cp>Go does not provide automatic stack traces like JavaScript, but you can use the \u003Ccode inline=\"\">runtime\u002Fdebug\u003C\u002Fcode> package:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">import (\n    \"errors\"\n    \"fmt\"\n    \"runtime\u002Fdebug\"\n)\n\nfunc main() {\n    err := errors.New(\"something went wrong\")\n    if err != nil {\n        fmt.Println(\"Error:\", err)\n        fmt.Println(string(debug.Stack()))\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Advantages:\u003C\u002Fstrong>\u003Cbr>✔️ Type-safe and ensures error handling at compile time\u003Cbr>✔️ Allows adding context using \u003Ccode inline=\"\">fmt.Errorf(\"context: %w\", err)\u003C\u002Fcode>\u003Cbr>✔️ Works well with logging libraries such as \u003Ccode inline=\"\">logrus\u003C\u002Fcode> or \u003Ccode inline=\"\">zap\u003C\u002Fcode>\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Limitations:\u003C\u002Fstrong>\u003Cbr>⚠️ Stack trace must be explicitly captured\u003Cbr>⚠️ Error handling requires returning and checking every error\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. Combining Logging with Errors\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>JavaScript + Logging (Winston)\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">const winston = require('winston');\n\nconst logger = winston.createLogger({\n  transports: [new winston.transports.Console()]\n});\n\ntry {\n  throw new Error(\"Something went wrong\");\n} catch (err) {\n  logger.error(err.message, { stack: err.stack });\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Go + Logging (Logrus)\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">import log \"github.com\u002Fsirupsen\u002Flogrus\"\n\nfunc main() {\n    err := errors.New(\"Something went wrong\")\n    if err != nil {\n        log.WithFields(log.Fields{\n            \"stack\": string(debug.Stack()),\n        }).Error(err)\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. Best Practices\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✔️ Fully utilize the \u003Cstrong>Error object \u002F error type\u003C\u002Fstrong>\u003Cbr>✔️ Add context to errors to make debugging easier\u003Cbr>✔️ Capture \u003Cstrong>stack traces\u003C\u002Fstrong> when errors occur\u003Cbr>✔️ Combine with \u003Cstrong>logging\u003C\u002Fstrong> to record events and error locations\u003Cbr>✔️ Separate error handling by environment:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cstrong>Development:\u003C\u002Fstrong> log + full stack trace\u003C\u002Fli>\u003Cli>\u003Cstrong>Production:\u003C\u002Fstrong> log summary + alerts\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>5. JavaScript vs Go: Error Handling Comparison\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Feature\u003C\u002Fth>\u003Cth>JavaScript\u003C\u002Fth>\u003Cth>Go\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Error Type\u003C\u002Ftd>\u003Ctd>Error object\u003C\u002Ftd>\u003Ctd>error interface\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Stack Trace\u003C\u002Ftd>\u003Ctd>Automatic (\u003Ccode inline=\"\">err.stack\u003C\u002Fcode>)\u003C\u002Ftd>\u003Ctd>\u003Ccode inline=\"\">runtime\u002Fdebug.Stack()\u003C\u002Fcode>\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Logging Integration\u003C\u002Ftd>\u003Ctd>Winston, Pino\u003C\u002Ftd>\u003Ctd>logrus, zap\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Error Context\u003C\u002Ftd>\u003Ctd>Can add properties dynamically\u003C\u002Ftd>\u003Ctd>Use \u003Ccode inline=\"\">fmt.Errorf\u003C\u002Fcode> or wrap errors\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Async Handling\u003C\u002Ftd>\u003Ctd>\u003Ccode inline=\"\">try\u002Fcatch + async\u002Fawait\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>Return error from function\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Recommendations:\u003C\u002Fstrong>\u003Cbr>✔️ \u003Cstrong>Web apps \u002F frontend-heavy\u003C\u002Fstrong> → JavaScript with stack trace + logging\u003Cbr>✔️ \u003Cstrong>Backend \u002F server\u003C\u002Fstrong> → Go with error + logging + stack trace\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Combining \u003Cstrong>stack trace\u003C\u002Fstrong> with \u003Cstrong>structured logging\u003C\u002Fstrong> allows professional debugging and monitoring in production environments.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Next Episode\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In \u003Cstrong>EP.29 of the JS2GO series\u003C\u002Fstrong>, we will learn \u003Cstrong>Signal and Process Management in Go and JavaScript\u003C\u002Fstrong> to control process lifecycle, handle signals, and manage application operations safely and correctly.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp data-start=\"498\" data-end=\"834\">\u003Cstrong>Read more\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Eps That Will Make You a Pro Tailwind CSS Overnight\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002Fen\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002Fen\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","56_11zon_komwjmv14d.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fpj9pdm7ftsq1b8y\u002F56_11zon_komwjmv14d.webp","2026-03-04 08:45:59.697Z","",{"keywords":15,"locale":43,"school_blog":53},[16,23,28,33,38],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:33:55.302Z","0mue4zt83jcdtq9","Logging","2026-04-10 16:08:03.396Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:45:58.499Z","8fjp9z3zgivqiqf","Stack Trace","2026-04-10 16:13:06.870Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:45:58.744Z","n49ukf60srps08s","Errors","2026-04-10 16:13:06.957Z",{"code":44,"collectionId":45,"collectionName":46,"created":47,"flag":48,"id":49,"is_default":50,"label":51,"updated":52},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":54,"collectionId":55,"collectionName":56,"expand":57,"id":71,"views":72},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":58},{"blogIds":59,"collectionId":60,"collectionName":61,"created":62,"created_by":13,"id":54,"image":63,"image_alt":13,"image_path":64,"label":65,"name":66,"priority":67,"publish_at":68,"scheduled_at":13,"status":69,"updated":70,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":66,"th":66},"JS2GO",10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","blwvy96xwsavt05",209,"pj9pdm7ftsq1b8y",[20,25,30,35,40],"2025-10-14 10:07:38.977Z","Learn how to handle errors in JavaScript and Go with Stack Trace and Logging to efficiently debug and monitor your applications. Includes code examples and best practices for production-ready error handling.","js2go-ep28-error-stacktrace-logging","2026-04-25 02:47:59.197Z",1,{"en":77}]