Skip to content

Commit 6d1a3bb

Browse files
waveywavesclaude
andcommitted
fix(crafter): surface auto-discovery errors instead of masking them
Fix variable shadowing in AddMaterialContactFreeWithAutoDetectedKind: m, err := (loop-scoped) → m, err = (outer-scoped) so the error from the last failed kind probe is propagated to the caller instead of always being nil. Also break early on protovalidate.ValidationError during auto-discovery so schema-level failures (e.g. invalid material name) are surfaced immediately instead of being masked by the kind-probing loop. Fixes: #2394 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Vibhav Bobade <vibhav.bobde@gmail.com>
1 parent 67e01b2 commit 6d1a3bb

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

pkg/attestation/crafter/crafter.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,30 +617,36 @@ func (c *Crafter) IsMaterialInContract(key string) bool {
617617
// AddMaterialContactFreeWithAutoDetectedKind adds a material to the crafting state checking the incoming material matches any of the
618618
// supported types in validation order. If the material is not found it will return an error.
619619
func (c *Crafter) AddMaterialContactFreeWithAutoDetectedKind(ctx context.Context, attestationID, name, value string, casBackend *casclient.CASBackend, runtimeAnnotations map[string]string) (*api.Attestation_Material, error) {
620-
var err error
620+
var (
621+
err error
622+
m *api.Attestation_Material
623+
)
621624
for _, kind := range schemaapi.CraftingMaterialInValidationOrder {
622-
m, err := c.AddMaterialContractFree(ctx, attestationID, kind.String(), name, value, casBackend, runtimeAnnotations)
625+
m, err = c.AddMaterialContractFree(ctx, attestationID, kind.String(), name, value, casBackend, runtimeAnnotations)
623626
if err == nil {
624-
// Successfully added material, return the kind
625627
return m, nil
626628
}
627629

628630
c.Logger.Debug().Err(err).Str("kind", kind.String()).Msg("failed to add material")
629631

630-
// Handle base error for upload and craft errors except the opening file error
631-
// TODO: have an error to detect validation error instead
632632
var policyError *policies.PolicyError
633633
if errors.Is(err, materials.ErrBaseUploadAndCraft) || errors.As(err, &policyError) {
634634
return nil, err
635635
}
636636

637-
// This is a final error, we detected the kind
638637
if v1.IsAttestationStateErrorConflict(err) {
639638
return nil, err
640639
}
640+
641+
// Proto-validation errors (e.g. invalid material name) are schema-level
642+
// failures, not kind mismatches. Stop probing immediately so the real
643+
// error is surfaced to the user instead of being masked by the loop.
644+
var valErr *protovalidate.ValidationError
645+
if errors.As(err, &valErr) {
646+
return nil, err
647+
}
641648
}
642649

643-
// Return an error if no material could be added
644650
return nil, fmt.Errorf("failed to auto-discover material kind: %w", err)
645651
}
646652

0 commit comments

Comments
 (0)