Skip to content

Commit 78661d7

Browse files
bouwkastclaude
andcommitted
Handle invalid assembly files with structured BAD_ASSEMBLY error
Wrap AssemblyBrowser construction in try-catch so corrupted or non-.NET DLLs return a structured error instead of crashing the CLI. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8d87a29 commit 78661d7

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

docs/development/InstrumentationGenerator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Machine-readable error codes:
338338
| `INVALID_CONFIG` | Bad JSON in `--config` or `--config-file` |
339339
| `UNKNOWN_KEY` | Unrecognized key in `--set` |
340340
| `GENERATION_ERROR` | Code generation failed |
341+
| `BAD_ASSEMBLY` | File is not a valid .NET assembly (corrupted, native, etc.) |
341342

342343
The `AMBIGUOUS_OVERLOAD` error is notable: it includes the full overload list in `data`, so you can immediately retry with `--overload-index` without a separate inspect call.
343344

docs/development/for-ai/InstrumentationGenerator-CLI.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ When `--json` is active, all errors are returned as structured JSON on stdout (n
212212
| `INVALID_CONFIG` | Bad JSON in `--config` or `--config-file` | Fix JSON syntax |
213213
| `UNKNOWN_KEY` | Bad key in `--set` | Use `--list-keys` to see valid keys |
214214
| `GENERATION_ERROR` | Code generation failed | Check error message for details |
215+
| `BAD_ASSEMBLY` | File is not a valid .NET assembly | Verify the file is a .NET DLL (not native, not corrupted) |
215216

216217
### AMBIGUOUS_OVERLOAD detail
217218

tracer/src/Datadog.AutoInstrumentation.Generator.Cli/Commands/GenerateCommand.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,21 @@ private int Execute(ParseResult parseResult)
186186
"Error: --config and --config-file are mutually exclusive. Use one or the other.");
187187
}
188188

189-
using var browser = new AssemblyBrowser(assemblyPath.FullName);
189+
AssemblyBrowser browser;
190+
try
191+
{
192+
browser = new AssemblyBrowser(assemblyPath.FullName);
193+
}
194+
catch (Exception ex)
195+
{
196+
return OutputHelper.WriteError(
197+
jsonMode,
198+
"generate",
199+
ErrorCodes.BadAssembly,
200+
$"Error: Failed to load assembly '{assemblyPath.Name}': {ex.Message}");
201+
}
202+
203+
using var _ = browser;
190204
var methodDef = browser.ResolveMethod(
191205
type,
192206
method,

tracer/src/Datadog.AutoInstrumentation.Generator.Cli/Commands/InspectCommand.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,21 @@ private int Execute(ParseResult parseResult)
168168
$"Error: Assembly file not found: {assemblyPath?.FullName ?? "<not specified>"}");
169169
}
170170

171-
using var browser = new AssemblyBrowser(assemblyPath.FullName);
171+
AssemblyBrowser browser;
172+
try
173+
{
174+
browser = new AssemblyBrowser(assemblyPath.FullName);
175+
}
176+
catch (Exception ex)
177+
{
178+
return OutputHelper.WriteError(
179+
jsonMode,
180+
"inspect",
181+
ErrorCodes.BadAssembly,
182+
$"Error: Failed to load assembly '{assemblyPath.Name}': {ex.Message}");
183+
}
184+
185+
using var _ = browser;
172186

173187
if (listTypes)
174188
{

tracer/src/Datadog.AutoInstrumentation.Generator.Cli/Output/ErrorCodes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ internal static class ErrorCodes
1515
public const string InvalidConfig = "INVALID_CONFIG";
1616
public const string UnknownKey = "UNKNOWN_KEY";
1717
public const string GenerationError = "GENERATION_ERROR";
18+
public const string BadAssembly = "BAD_ASSEMBLY";
1819
}

0 commit comments

Comments
 (0)