chore: create interface for pkgs to return codersdk errors (#18719)

This interface allows it to create rich codersdk errors and pass them up to the `wsbuilder` error handling.
This commit is contained in:
Steven Masley
2025-07-03 08:33:45 -05:00
committed by GitHub
parent 7d412c2272
commit 699dd8e554
6 changed files with 182 additions and 119 deletions
+10
View File
@@ -240,6 +240,9 @@ type BuildError struct {
}
func (e BuildError) Error() string {
if e.Wrapped == nil {
return e.Message
}
return e.Wrapped.Error()
}
@@ -247,6 +250,13 @@ func (e BuildError) Unwrap() error {
return e.Wrapped
}
func (e BuildError) Response() (int, codersdk.Response) {
return e.Status, codersdk.Response{
Message: e.Message,
Detail: e.Error(),
}
}
// Build computes and inserts a new workspace build into the database. If authFunc is provided, it also performs
// authorization preflight checks.
func (b *Builder) Build(
+18
View File
@@ -12,6 +12,7 @@ import (
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/files"
"github.com/coder/coder/v2/coderd/httpapi/httperror"
"github.com/coder/coder/v2/provisionersdk"
"github.com/google/uuid"
@@ -1000,6 +1001,23 @@ func TestWorkspaceBuildDeleteOrphan(t *testing.T) {
})
}
func TestWsbuildError(t *testing.T) {
t.Parallel()
const msg = "test error"
var buildErr error = wsbuilder.BuildError{
Status: http.StatusBadRequest,
Message: msg,
}
respErr, ok := httperror.IsResponder(buildErr)
require.True(t, ok, "should be a Coder SDK error")
code, resp := respErr.Response()
require.Equal(t, http.StatusBadRequest, code)
require.Equal(t, msg, resp.Message)
}
type txExpect func(mTx *dbmock.MockStore)
func expectDB(t *testing.T, opts ...txExpect) *dbmock.MockStore {