Skip to content

fix: coderd: putExtendWorkspace: move error from validation to message #3289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: coderd: extract error messages to variables
  • Loading branch information
johnstcn committed Jul 29, 2022
commit da29766239b24823a3d9dd71b02a8a9136de9e6a
25 changes: 18 additions & 7 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ import (

const workspaceDefaultTTL = 2 * time.Hour

var (
ttlMin = time.Minute
ttlMax = 7 * 24 * time.Hour

errTTLMin = xerrors.New("time until shutdown must be at least one minute")
errTTLMax = xerrors.New("time until shutdown must be less than 7 days")
errDeadlineTooSoon = xerrors.New("new deadline must be at least 30 minutes in the future")
errDeadlineBeforeStart = xerrors.New("new deadline must be before workspace start time")
errDeadlineOverTemplateMax = xerrors.New("new deadline is greater than template allows")
)

func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)
if !api.Authorize(r, rbac.ActionRead, workspace) {
Expand Down Expand Up @@ -894,12 +905,12 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e

dur := time.Duration(*millis) * time.Millisecond
truncated := dur.Truncate(time.Minute)
if truncated < time.Minute {
return sql.NullInt64{}, xerrors.New("time until shutdown must be at least one minute")
if truncated < ttlMin {
return sql.NullInt64{}, errTTLMin
}

if truncated > 24*7*time.Hour {
return sql.NullInt64{}, xerrors.New("time until shutdown must be less than 7 days")
if truncated > ttlMax {
return sql.NullInt64{}, errTTLMax
}

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

// No idea how this could happen.
if newDeadline.Before(startedAt) {
return xerrors.Errorf("new deadline must be before workspace start time")
return errDeadlineBeforeStart
}

delta := newDeadline.Sub(startedAt)
if delta > max {
return xerrors.New("new deadline is greater than template allows")
return errDeadlineOverTemplateMax
}

return nil
Expand Down