# \[ Error handling \] How to know the code error line number?

**URL:** <https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393>\
**Category:** UXP Plugin API\
**Tags:** uxp, javascript\
**Created:** [June 11, 2023, 1:20am UTC](https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393 "2023-06-11T01:20:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![photonic](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/photonic/32/2513_2.png) [@photonic](https://forums.creativeclouddeveloper.com/u/photonic)\
**Post date:** [June 11, 2023, 1:20am UTC](https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393/1 "2023-06-11T01:20:59Z")

</div>

In CEP we had the `err.line` property for the `err` in a try / catch block:

```auto
try {
// SOME CODE
 } catch(err) { 
console.log(err.line);
}

```

So it was easy to track the line code where the error was happening, however with UXP there’s no `err.line` or at least I haven’t find a way, what is the solution for this or an alternative?

I’ve tried `err.stack` but sometimes it doesn’t throws the error line.

Thanks!

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [June 11, 2023, 7:31am UTC](https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393/2 "2023-06-11T07:31:38Z")

</div>

Use `console.trace(err)` instead.

> **[console: trace() static method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/console/trace_static)**
>
> The console.trace() static method outputs a stack trace to the console.

---

<div class="post-metadata">

**Author:** ![photonic](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/photonic/32/2513_2.png) [@photonic](https://forums.creativeclouddeveloper.com/u/photonic)\
**Post date:** [June 14, 2023, 12:09am UTC](https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393/3 "2023-06-14T00:09:26Z")

</div>

Thanks for the answer it works! Now If I want to “alert” this `err` doesn’t give me a way to trace the line error. Do you have any suggestions?

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [June 14, 2023, 10:43am UTC](https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393/4 "2023-06-14T10:43:12Z")

</div>

You can get the stack trace as a string with `err.stack`.  
There’s no method to return just the line reference afaik, but you could do something like this to mutate the string to an array and then alert the relevant element.

```auto
const formatStackTrace = (stack) => {
  return stack.split("\n").map(line => line.trim());
};

// ...
catch (err) {
  const stackTraceArr = formatStackTrace(err.stack)
  alert(stackTraceArr[1])
}

```

---

<div class="post-metadata">

**Author:** ![cameronmcefee](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/cameronmcefee/32/2173_2.png) [@cameronmcefee](https://forums.creativeclouddeveloper.com/u/cameronmcefee)\
**Post date:** [June 14, 2023, 8:09pm UTC](https://forums.creativeclouddeveloper.com/t/error-handling-how-to-know-the-code-error-line-number/6393/5 "2023-06-14T20:09:38Z")

</div>

It’s worth considering what and why you’re erroring. You mentioned using Alerts. Alerts are generally for users, and users don’t care about exceptions — they want human formatted error messages, which you write yourself, that are specific to how they should respond, not the code that caused them.

If you’re just trying to do local debugging, use console.error(yourError). The chromium console will print most of the stack trace for you without any additional work, and if you’ve got sourcemaps hooked up, they’ll link right to the source.

And for remote debugging, I strongly recommend using something like [https://sentry.io/](https://sentry.io/). That way you get notified of your production errors and they point specifically at the code that caused the issues. Setting up Sentry is one of the first things I do on every project and write code in a way that specifically accounts for errors and sends them to Sentry with the relevant context.
