@@ -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,28 +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
+ var validErr codersdk.ValidationError
551
+ if errors .As (err , & validErr ) {
552
+ httpapi .Write (rw , http .StatusBadRequest , codersdk.Response {
553
+ Message : "Error updating workspace time until shutdown!" ,
554
+ Validations : []codersdk.ValidationError {validErr },
555
+ })
556
+ return
556
557
}
557
- httpapi . Write ( rw , code , codersdk. Response {
558
- Message : "Error updating workspace time until shutdown!" ,
559
- Validations : validErrs ,
560
- Detail : err .Error (),
558
+
559
+ httpapi . Write ( rw , http . StatusInternalServerError , codersdk. Response {
560
+ Message : "Error updating workspace time until shutdown!" ,
561
+ Detail : err .Error (),
561
562
})
562
563
return
563
564
}
@@ -895,15 +896,15 @@ func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, e
895
896
dur := time .Duration (* millis ) * time .Millisecond
896
897
truncated := dur .Truncate (time .Minute )
897
898
if truncated < time .Minute {
898
- return sql.NullInt64 {}, xerrors .New ("ttl must be at least one minute" )
899
+ return sql.NullInt64 {}, xerrors .New ("time until shutdown must be at least one minute" )
899
900
}
900
901
901
902
if truncated > 24 * 7 * time .Hour {
902
- return sql.NullInt64 {}, xerrors .New ("ttl must be less than 7 days" )
903
+ return sql.NullInt64 {}, xerrors .New ("time until shutdown must be less than 7 days" )
903
904
}
904
905
905
906
if truncated > max {
906
- return sql.NullInt64 {}, xerrors .Errorf ("ttl must be below template maximum %s" , max .String ())
907
+ return sql.NullInt64 {}, xerrors .Errorf ("time until shutdown must be below template maximum %s" , max .String ())
907
908
}
908
909
909
910
return sql.NullInt64 {
0 commit comments