Skip to content

Commit 4658b3f

Browse files
authored
fix: coderd: putExtendWorkspace: move error from validation to message (#3289)
* refactor: coderd: extract error messages to variables * fix: putExtendWorkspace: return validation error in message field
1 parent 74c8766 commit 4658b3f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

coderd/workspaces.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import (
3434

3535
const workspaceDefaultTTL = 2 * time.Hour
3636

37+
var (
38+
ttlMin = time.Minute //nolint:revive // min here means 'minimum' not 'minutes'
39+
ttlMax = 7 * 24 * time.Hour
40+
41+
errTTLMin = xerrors.New("time until shutdown must be at least one minute")
42+
errTTLMax = xerrors.New("time until shutdown must be less than 7 days")
43+
errDeadlineTooSoon = xerrors.New("new deadline must be at least 30 minutes in the future")
44+
errDeadlineBeforeStart = xerrors.New("new deadline must be before workspace start time")
45+
errDeadlineOverTemplateMax = xerrors.New("new deadline is greater than template allows")
46+
)
47+
3748
func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
3849
workspace := httpmw.WorkspaceParam(r)
3950
if !api.Authorize(r, rbac.ActionRead, workspace) {
@@ -623,9 +634,11 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) {
623634

624635
newDeadline := req.Deadline.UTC()
625636
if err := validWorkspaceDeadline(job.CompletedAt.Time, newDeadline, time.Duration(template.MaxTtl)); err != nil {
637+
// NOTE(Cian): Putting the error in the Message field on request from the FE folks.
638+
// Normally, we would put the validation error in Validations, but this endpoint is
639+
// not tied to a form or specific named user input on the FE.
626640
code = http.StatusBadRequest
627-
resp.Message = "Bad extend workspace request."
628-
resp.Validations = append(resp.Validations, codersdk.ValidationError{Field: "deadline", Detail: err.Error()})
641+
resp.Message = "Cannot extend workspace: " + err.Error()
629642
return err
630643
}
631644

@@ -894,12 +907,12 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e
894907

895908
dur := time.Duration(*millis) * time.Millisecond
896909
truncated := dur.Truncate(time.Minute)
897-
if truncated < time.Minute {
898-
return sql.NullInt64{}, xerrors.New("time until shutdown must be at least one minute")
910+
if truncated < ttlMin {
911+
return sql.NullInt64{}, errTTLMin
899912
}
900913

901-
if truncated > 24*7*time.Hour {
902-
return sql.NullInt64{}, xerrors.New("time until shutdown must be less than 7 days")
914+
if truncated > ttlMax {
915+
return sql.NullInt64{}, errTTLMax
903916
}
904917

905918
if truncated > max {
@@ -915,17 +928,17 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e
915928
func validWorkspaceDeadline(startedAt, newDeadline time.Time, max time.Duration) error {
916929
soon := time.Now().Add(29 * time.Minute)
917930
if newDeadline.Before(soon) {
918-
return xerrors.New("new deadline must be at least 30 minutes in the future")
931+
return errDeadlineTooSoon
919932
}
920933

921934
// No idea how this could happen.
922935
if newDeadline.Before(startedAt) {
923-
return xerrors.Errorf("new deadline must be before workspace start time")
936+
return errDeadlineBeforeStart
924937
}
925938

926939
delta := newDeadline.Sub(startedAt)
927940
if delta > max {
928-
return xerrors.New("new deadline is greater than template allows")
941+
return errDeadlineOverTemplateMax
929942
}
930943

931944
return nil

coderd/workspaces_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1063,14 +1063,15 @@ func TestWorkspaceExtend(t *testing.T) {
10631063
err = client.PutExtendWorkspace(ctx, workspace.ID, codersdk.PutExtendWorkspaceRequest{
10641064
Deadline: deadlineTooSoon,
10651065
})
1066-
require.ErrorContains(t, err, "new deadline must be at least 30 minutes in the future", "setting a deadline less than 30 minutes in the future should fail")
1066+
require.ErrorContains(t, err, "unexpected status code 400: Cannot extend workspace: new deadline must be at least 30 minutes in the future", "setting a deadline less than 30 minutes in the future should fail")
10671067

10681068
// And with a deadline greater than the template max_ttl should also fail
10691069
deadlineExceedsMaxTTL := time.Now().Add(time.Duration(template.MaxTTLMillis) * time.Millisecond).Add(time.Minute)
10701070
err = client.PutExtendWorkspace(ctx, workspace.ID, codersdk.PutExtendWorkspaceRequest{
10711071
Deadline: deadlineExceedsMaxTTL,
10721072
})
1073-
require.ErrorContains(t, err, "new deadline is greater than template allows", "setting a deadline greater than that allowed by the template should fail")
1073+
1074+
require.ErrorContains(t, err, "unexpected status code 400: Cannot extend workspace: new deadline is greater than template allows", "setting a deadline greater than that allowed by the template should fail")
10741075

10751076
// Updating with a deadline 30 minutes in the future should succeed
10761077
deadlineJustSoonEnough := time.Now().Add(30 * time.Minute)

0 commit comments

Comments
 (0)