From 70537aab08e94b75de1667f824bc1ca8bc15b606 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 5 Mar 2024 17:58:01 -0800 Subject: [PATCH] backendbase: ErrorAsDiagnostics The legacy helper/schema based Backend implementation expected the backend-specific code to return error and then wrapped it in diagnostics itself before returning. Backends using backendbase are supposed to return diagnostics directly themselves, but having to manually replace every error case with a higher-quality diagnostic message would get in the way of EOLing the legacy helper/schema implementation, and so this helper function is a pragmatic compromise to allow us to keep those updates purely mechanical for now and then improve diagnostics as a separate effort later. --- internal/backend/backendbase/helper.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/internal/backend/backendbase/helper.go b/internal/backend/backendbase/helper.go index 41527c6359..bd03c67d73 100644 --- a/internal/backend/backendbase/helper.go +++ b/internal/backend/backendbase/helper.go @@ -92,3 +92,28 @@ func GetAttrEnvDefaultFallback(v cty.Value, attrName string, defEnv string, fall } return ret } + +// ErrorAsDiagnostics wraps a non-nil error as a tfdiags.Diagnostics. +// +// Panics if the given error is nil, since the caller should only be using +// this if they've encountered a non-nil error. +// +// This is here just as a temporary measure to preserve the old treatment of +// errors returned from legacy helper/schema-based backend implementations, +// so that we can minimize the churn caused in the first iteration of adopting +// backendbase. +// +// In the long run backends should produce higher-quality diagnostics directly +// themselves, but we wanted to first complete the deprecation of the +// legacy/helper/schema package with only mechanical code updates and then +// save diagnostic quality improvements for a later time. +func ErrorAsDiagnostics(err error) tfdiags.Diagnostics { + if err == nil { + panic("ErrorAsDiagnostics with nil error") + } + var diags tfdiags.Diagnostics + // This produces a very low-quality diagnostic message, but it matches + // how legacy helper/schema dealt with the same situation. + diags = diags.Append(err) + return diags +}