@@ -288,7 +288,7 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
288288 dbTTL , err := validWorkspaceTTLMillis (createWorkspace .TTLMillis , time .Duration (template .MaxTtl ))
289289 if err != nil {
290290 httpapi .Write (rw , http .StatusBadRequest , codersdk.Response {
291- Message : "Invalid Workspace TTL ." ,
291+ Message : "Invalid Workspace Time to Shutdown ." ,
292292 Validations : []codersdk.ValidationError {{Field : "ttl_ms" , Detail : err .Error ()}},
293293 })
294294 return
@@ -523,8 +523,6 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
523523 return
524524 }
525525
526- var validErrs []codersdk.ValidationError
527-
528526 err := api .Database .InTx (func (s database.Store ) error {
529527 template , err := s .GetTemplateByID (r .Context (), workspace .TemplateID )
530528 if err != nil {
@@ -536,29 +534,31 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
536534
537535 dbTTL , err := validWorkspaceTTLMillis (req .TTLMillis , time .Duration (template .MaxTtl ))
538536 if err != nil {
539- validErrs = append (validErrs , codersdk.ValidationError {Field : "ttl_ms" , Detail : err .Error ()})
540- return err
537+ return codersdk.ValidationError {Field : "ttl_ms" , Detail : err .Error ()}
541538 }
542539 if err := s .UpdateWorkspaceTTL (r .Context (), database.UpdateWorkspaceTTLParams {
543540 ID : workspace .ID ,
544541 Ttl : dbTTL ,
545542 }); err != nil {
546- return xerrors .Errorf ("update workspace TTL : %w" , err )
543+ return xerrors .Errorf ("update workspace time until shutdown : %w" , err )
547544 }
548545
549546 return nil
550547 })
551548
552549 if err != nil {
553- code := http .StatusInternalServerError
554- if len (validErrs ) > 0 {
555- code = http .StatusBadRequest
550+ resp := codersdk.Response {
551+ Message : "Error updating workspace time until shutdown." ,
556552 }
557- httpapi .Write (rw , code , codersdk.Response {
558- Message : "Error updating workspace time until shutdown!" ,
559- Validations : validErrs ,
560- Detail : err .Error (),
561- })
553+ var validErr codersdk.ValidationError
554+ if errors .As (err , & validErr ) {
555+ resp .Validations = []codersdk.ValidationError {validErr }
556+ httpapi .Write (rw , http .StatusBadRequest , resp )
557+ return
558+ }
559+
560+ resp .Detail = err .Error ()
561+ httpapi .Write (rw , http .StatusInternalServerError , resp )
562562 return
563563 }
564564
@@ -895,15 +895,15 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e
895895 dur := time .Duration (* millis ) * time .Millisecond
896896 truncated := dur .Truncate (time .Minute )
897897 if truncated < time .Minute {
898- return sql.NullInt64 {}, xerrors .New ("ttl must be at least one minute" )
898+ return sql.NullInt64 {}, xerrors .New ("time until shutdown must be at least one minute" )
899899 }
900900
901901 if truncated > 24 * 7 * time .Hour {
902- return sql.NullInt64 {}, xerrors .New ("ttl must be less than 7 days" )
902+ return sql.NullInt64 {}, xerrors .New ("time until shutdown must be less than 7 days" )
903903 }
904904
905905 if truncated > max {
906- return sql.NullInt64 {}, xerrors .Errorf ("ttl must be below template maximum %s" , max .String ())
906+ return sql.NullInt64 {}, xerrors .Errorf ("time until shutdown must be below template maximum %s" , max .String ())
907907 }
908908
909909 return sql.NullInt64 {
0 commit comments