From 89471826eafe076e90fdfb93c69884d82fb52893 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Wed, 25 Nov 2026 10:32:20 +0900 Subject: internal/rosa: replace non-error panics These used to never accept user input, so unrecovered panics were considered an acceptable way to notify a bad API usage. These now accept user input via registered azalea files, so make them more elegant by panicking errors only, which enables error recovery via the azalea evaluator, and enables better error messages pointing to azalea source code, if the error originated from azalea. Signed-off-by: Ophestra --- internal/rosa/go.go | 2 +- internal/rosa/rosa.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/rosa/go.go b/internal/rosa/go.go index 866ee094..b0cf5c81 100644 --- a/internal/rosa/go.go +++ b/internal/rosa/go.go @@ -108,7 +108,7 @@ mkdir -p /var/tmp/ finalEnv = append(finalEnv, "CGO_ENABLED=0") default: - panic("unsupported target " + arch) + panic(TargetError(arch)) } go119 := t.newGo( diff --git a/internal/rosa/rosa.go b/internal/rosa/rosa.go index bb9dc317..22dbcd6d 100644 --- a/internal/rosa/rosa.go +++ b/internal/rosa/rosa.go @@ -1,6 +1,7 @@ package rosa import ( + "errors" "os" "slices" "strconv" @@ -59,6 +60,13 @@ var ( AbsSystem = fhs.AbsRoot.Append(prefixR) ) +// TargetError is an unsupported target architecture name. +type TargetError string + +func (e TargetError) Error() string { + return "unsupported target " + string(e) +} + // linuxArch returns the architecture name used by linux corresponding to arch. func linuxArch(arch string) string { switch arch { @@ -71,7 +79,7 @@ func linuxArch(arch string) string { return arch default: - panic("unsupported target " + arch) + panic(TargetError(arch)) } } @@ -429,6 +437,24 @@ func (e BootstrapError) Error() string { " must not be built by bootstrap stages" } +// A StageError describes an out-of-bounds stage value. +type StageError Stage + +func (e StageError) Error() string { + return "invalid stage " + strconv.Itoa(int(e)) +} + +var ( + // ErrBadName is returned by [Toolchain.New] attempting to create a package + // with an empty name or version string. + ErrBadName = errors.New("name must be non-empty") + + // ErrChdir is returned by [Toolchain.New] if neither the package attributes + // nor the resolved helper request a directory, and the early script segment + // is not populated. + ErrChdir = errors.New("cannot remain in root") +) + // New constructs a [pkg.Artifact] via a build system helper. func (t Toolchain) New( name, version string, @@ -450,7 +476,7 @@ func (t Toolchain) New( } if name == "" || version == "" { - panic("name must be non-empty") + panic(ErrBadName) } rn := name if version != Unversioned { @@ -533,7 +559,7 @@ func (t Toolchain) New( }, Prefix+"/bin", "/bin") default: - panic("unsupported toolchain " + strconv.Itoa(int(t.stage))) + panic(StageError(t.stage)) } wantsChmod, wantsWrite := helper.wantsChmod(), helper.wantsWrite() @@ -616,7 +642,7 @@ cd '/usr/src/` + name + `/' } scriptEarly += "\ncd " + dir + "\n" } else if !attr.EnterSource && attr.ScriptEarly == "" && dir != "" { - panic("cannot remain in root") + panic(ErrChdir) } paths := attr.Paths -- cgit v1.3.1