@@ -288,7 +288,7 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
288
288
dbTTL , err := validWorkspaceTTLMillis (createWorkspace .TTLMillis , time .Duration (template .MaxTtl ))
289
289
if err != nil {
290
290
httpapi .Write (rw , http .StatusBadRequest , codersdk.Response {
291
- Message : "Invalid Workspace TTL ." ,
291
+ Message : "Invalid Workspace Time to Shutdown ." ,
292
292
Validations : []codersdk.ValidationError {{Field : "ttl_ms" , Detail : err .Error ()}},
293
293
})
294
294
return
@@ -523,8 +523,6 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
523
523
return
524
524
}
525
525
526
- var validErrs []codersdk.ValidationError
527
-
528
526
err := api .Database .InTx (func (s database.Store ) error {
529
527
template , err := s .GetTemplateByID (r .Context (), workspace .TemplateID )
530
528
if err != nil {
@@ -536,29 +534,31 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
536
534
537
535
dbTTL , err := validWorkspaceTTLMillis (req .TTLMillis , time .Duration (template .MaxTtl ))
538
536
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 ()}
541
538
}
542
539
if err := s .UpdateWorkspaceTTL (r .Context (), database.UpdateWorkspaceTTLParams {
543
540
ID : workspace .ID ,
544
541
Ttl : dbTTL ,
545
542
}); err != nil {
546
- return xerrors .Errorf ("update workspace TTL : %w" , err )
543
+ return xerrors .Errorf ("update workspace time until shutdown : %w" , err )
547
544
}
548
545
549
546
return nil
550
547
})
551
548
552
549
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." ,
556
552
}
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 )
562
562
return
563
563
}
564
564
@@ -895,15 +895,15 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e
895
895
dur := time .Duration (* millis ) * time .Millisecond
896
896
truncated := dur .Truncate (time .Minute )
897
897
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" )
899
899
}
900
900
901
901
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" )
903
903
}
904
904
905
905
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 ())
907
907
}
908
908
909
909
return sql.NullInt64 {
0 commit comments