Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4658b3f

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 parent74c8766 commit4658b3f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

‎coderd/workspaces.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import (
3434

3535
constworkspaceDefaultTTL=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
iferr:=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
returnerr
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-
iftruncated<time.Minute {
898-
return sql.NullInt64{},xerrors.New("time until shutdown must be at least one minute")
910+
iftruncated<ttlMin {
911+
return sql.NullInt64{},errTTLMin
899912
}
900913

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

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

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

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

931944
returnnil

‎coderd/workspaces_test.go

Lines changed: 3 additions & 2 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp