Skip to content

Add decorator hook for CLR exception error objects#2280

Merged
lahma merged 2 commits into
mainfrom
copilot/add-error-object-decoration
Feb 12, 2026
Merged

Add decorator hook for CLR exception error objects#2280
lahma merged 2 commits into
mainfrom
copilot/add-error-object-decoration

Conversation

Copilot AI commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

Enables post-processing of JavaScript error objects created from caught CLR exceptions. Addresses use cases like adding CLR type information, stack traces, or custom context properties to errors.

Changes

  • New delegate: ClrExceptionErrorDecoratorDelegate(Engine, ObjectInstance error, Exception clrException) in Options.cs
  • Configuration: ClrExceptionErrorDecorator property in Options.InteropOptions, invoked only when ExceptionHandler catches the exception
  • Error creation: Throw.FromClrException() centralizes error construction and decorator invocation, used by ClrFunction.CallSlow() and Throw.MeaningfulException()
  • Fluent API: DecorateClrExceptionErrors() extension method for configuration

Usage

var engine = new Engine(options =>
{
    options.CatchClrExceptions();
    options.DecorateClrExceptionErrors((engine, error, clrException) =>
    {
        error.Set("clrType", clrException.GetType().FullName);
        error.Set("clrStackTrace", clrException.StackTrace);
    });
});

engine.SetValue("throw", new Action(() => throw new InvalidOperationException("test")));

engine.Execute(@"
    try { throw(); } 
    catch(e) { console.log(e.clrType); }  // System.InvalidOperationException
");

Decorator is called after base error object creation, allowing enrichment without full replacement. No decorator overhead when not configured.

Original prompt

This section details on the original issue you should resolve

<issue_title>Decorate error object from CLR</issue_title>
<issue_description>We probably need a new option for handling exception enrichening. Now we have the catch CLR exception option but also a way to post-process created exception.

Discussed in #2278

Originally posted by kxs-rgamezdiaz February 11, 2026
Is it possible to add custom properties to the error object that gets caught in js?

Let's say I want to make the following test pass. The idea is to send an "errorCode" property from the CLR.

[TestClass]
public sealed class ScriptEngineExceptionTests
{
    [TestMethod]
    public void ErrorShouldContainProperties()
    {
        Engine engine = new(options =>
        {
            options.CatchClrExceptions();
        });

        engine.SetValue("myClass", new());

        engine.Execute("""
            function main () {
                try {
                    myClass.MethodThatThrows();
                } catch (e) {
                    return JSON.stringify(e);
                }
            }
            """);

        string result = engine.Invoke("main").ToString();

        Assert.AreEqual("{errorCode:'InvalidOperation'}", result);
    }

    public class MyClass()
    {
        public void MethodThatThrows() => throw new InvalidOperationException("This is a test exception.");
    }
}

Right now the Invoke returns an empty object {}.

Thanks!

</issue_description>

<agent_instructions>Think of ways to handle this the way it's sustainable and allows extension. Jint could just create the base exception and let a hook post-process it. Allowing full exception creation might be too much, but analyze.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot AI changed the title [WIP] Add option to decorate error object from CLR Add decorator hook for CLR exception error objects Feb 11, 2026
Copilot AI requested a review from lahma February 11, 2026 21:29
@lahma
lahma marked this pull request as ready for review February 12, 2026 15:03
@lahma
lahma merged commit 36d4824 into main Feb 12, 2026
4 checks passed
@lahma
lahma deleted the copilot/add-error-object-decoration branch February 12, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decorate error object from CLR

2 participants