Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8025488

Error.captureStackTrace should not format error stack

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 8
    • Fix Version/s: 8
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b112
    • CPU:
      generic
    • OS:
      generic

      Description

      Calling captureStackTrace has the side effect to call toString on the error instance. If called during initialization we end calling toString on an instance that is not fully initialised.

      This issue impacts (at least) mongoose node module document validation.

      From v8 StackTrace API:
      Error.captureStackTrace(error, constructorOpt)
      adds a stack property to the given error object that will yield the stack trace at the time captureStackTrace was called. The reason for not just returning the formatted stack trace directly is that this way we can postpone the formatting of the stack trace until the stack property is accessed and avoid formatting completely if it never is.

      Test case:

      function MyError () {
        Error.call(this);
        // Side effect calls toString on non fully initialized instance.
        Error.captureStackTrace(this);
        this.arr = {};
      };

      MyError.prototype.toString = function() {
          Object.keys(this.arr).forEach(function (key) {
        }, this)
      }

      var e = new MyError();

        Attachments

          Activity

            People

            Assignee:
            sundar Sundararajan Athijegannathan
            Reporter:
            jfdenise Jean-Francois Denise (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: