Metalama icon indicating copy to clipboard operation
Metalama copied to clipboard

Feature: Improve error messages to include the offending line in the aspect

Open WhitWaldo opened this issue 1 year ago • 4 comments

I'm applying a TypeAspect to a class that looks like:

public class MyDoodad<T>
{
}

My aspect starts by assigning the type parameters to separate variables. Herein was my problem - I referenced the first and second type parameters at the top:

var typeA = builder.Target.TypeParameters[0];
var typeB = builder.Target.TypeParameters[1];

In turn, when trying to look at the Metalama preview, I received the following error:

Cannot preview the transformed code I:\...\MyObject.cs(6, 16) error LAMA0041: 'MyAspect.BuildAspect' threw 'ArgumentOutOfRangeException' when applied to 'MyObject<T>'; Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

...and so on.

I had a lot of range accessors in my aspect so narrowing this down took a little while (lots of time staring at the wrong sections).

Yes, the issue itself is spot on, but I might suggest that instead of indicating the type the aspect is applied to as the source of the error, this should instead be enriched to indicate where in the aspect itself is the source of the error, e.g. "error LAMA0041: 'MyAspect.BuildAspect' at 'builder.Target.TypeParameters[1]' threw 'ArgumentOutOfRangeException' when applied" as this would have dramatically improved the turnaround time on identifying a fix.

Thanks for the consideration!

WhitWaldo avatar May 06 '23 09:05 WhitWaldo