Skip to content

Commit

Permalink
Retain backtrace for TypeErrors in value change callback (#584)
Browse files Browse the repository at this point in the history
This code was originally added to provide better error messaging
for TypeErrors when assigning values.

Unfortunately throwing a new error object causes us to lose
the context (including stacktrace) of the original error,
making it harder to locate the source.

Changing the message and rethrowing the original error
retain that information.
  • Loading branch information
elliotcm committed Sep 15, 2022
1 parent a2a030f commit 11e4c16
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/value_observer.ts
Expand Up @@ -96,9 +96,11 @@ export class ValueObserver implements StringMapObserverDelegate {

changedMethod.call(this.receiver, value, oldValue)
} catch (error) {
if (!(error instanceof TypeError)) throw error
if (error instanceof TypeError) {
error.message = `Stimulus Value "${this.context.identifier}.${descriptor.name}" - ${error.message}`
}

throw new TypeError(`Stimulus Value "${this.context.identifier}.${descriptor.name}" - ${error.message}`)
throw error
}
}
}
Expand Down

0 comments on commit 11e4c16

Please sign in to comment.