<!--
title: safe error data handling
domain: app-frameworks
topic: Error Handling
language: TypeScript
source: nuxt/nuxt
updated: 2025-03-28
url: https://awesomereviewers.com/reviewers/nuxt-safe-error-data-handling/
-->

When processing error data that may contain unsafe or undefined values, use safe parsing methods and defensive programming techniques to prevent secondary exceptions during error handling.

Use safe JSON parsing libraries like `destr()` instead of `JSON.parse()` when parsing potentially malformed JSON data from error objects:

```javascript
// Instead of:
ssrError.data = JSON.parse(ssrError.data)

// Use:
ssrError.data = destr(ssrError.data)
```

Apply destructuring with fallback values when accessing potentially undefined error data:

```javascript
// Instead of:
const caller = stack[stack.length - 1]
const explanation = `${fileURLToPath(caller.source)}:${caller.line}:${caller.column}`

// Use:
const {source, line, column} = stack[stack.length - 1] ?? {}
const explanation = source ? ` (used at ${fileURLToPath(source)}:${line}:${column})` : ''
```

This approach prevents error handling code from becoming a source of additional errors, ensuring that the original error information is preserved and properly communicated to users or logging systems.
