@@ -102,14 +102,23 @@ func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
102
102
})
103
103
return
104
104
}
105
- httpapi .Write (ctx , rw , http .StatusOK , convertWorkspace (
105
+
106
+ w , err := convertWorkspace (
106
107
apiKey .UserID ,
107
108
workspace ,
108
109
data .builds [0 ],
109
110
data .templates [0 ],
110
111
ownerName ,
111
112
api .Options .AllowWorkspaceRenames ,
112
- ))
113
+ )
114
+ if err != nil {
115
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
116
+ Message : "Internal error converting workspace." ,
117
+ Detail : err .Error (),
118
+ })
119
+ return
120
+ }
121
+ httpapi .Write (ctx , rw , http .StatusOK , w )
113
122
}
114
123
115
124
// workspaces returns all workspaces a user can read.
@@ -280,14 +289,22 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
280
289
})
281
290
return
282
291
}
283
- httpapi . Write ( ctx , rw , http . StatusOK , convertWorkspace (
292
+ w , err := convertWorkspace (
284
293
apiKey .UserID ,
285
294
workspace ,
286
295
data .builds [0 ],
287
296
data .templates [0 ],
288
297
ownerName ,
289
298
api .Options .AllowWorkspaceRenames ,
290
- ))
299
+ )
300
+ if err != nil {
301
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
302
+ Message : "Internal error converting workspace." ,
303
+ Detail : err .Error (),
304
+ })
305
+ return
306
+ }
307
+ httpapi .Write (ctx , rw , http .StatusOK , w )
291
308
}
292
309
293
310
// Create a new workspace for the currently authenticated user.
@@ -590,14 +607,22 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
590
607
return
591
608
}
592
609
593
- httpapi . Write ( ctx , rw , http . StatusCreated , convertWorkspace (
610
+ w , err := convertWorkspace (
594
611
apiKey .UserID ,
595
612
workspace ,
596
613
apiBuild ,
597
614
template ,
598
615
member .Username ,
599
616
api .Options .AllowWorkspaceRenames ,
600
- ))
617
+ )
618
+ if err != nil {
619
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
620
+ Message : "Internal error converting workspace." ,
621
+ Detail : err .Error (),
622
+ })
623
+ return
624
+ }
625
+ httpapi .Write (ctx , rw , http .StatusCreated , w )
601
626
}
602
627
603
628
// @Summary Update workspace metadata by ID
@@ -931,14 +956,23 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) {
931
956
}
932
957
933
958
aReq .New = workspace
934
- httpapi .Write (ctx , rw , http .StatusOK , convertWorkspace (
959
+
960
+ w , err := convertWorkspace (
935
961
apiKey .UserID ,
936
962
workspace ,
937
963
data .builds [0 ],
938
964
data .templates [0 ],
939
965
ownerName ,
940
966
api .Options .AllowWorkspaceRenames ,
941
- ))
967
+ )
968
+ if err != nil {
969
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
970
+ Message : "Internal error converting workspace." ,
971
+ Detail : err .Error (),
972
+ })
973
+ return
974
+ }
975
+ httpapi .Write (ctx , rw , http .StatusOK , w )
942
976
}
943
977
944
978
// @Summary Extend workspace deadline by ID
@@ -1349,16 +1383,27 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
1349
1383
})
1350
1384
return
1351
1385
}
1386
+
1387
+ w , err := convertWorkspace (
1388
+ apiKey .UserID ,
1389
+ workspace ,
1390
+ data .builds [0 ],
1391
+ data .templates [0 ],
1392
+ ownerName ,
1393
+ api .Options .AllowWorkspaceRenames ,
1394
+ )
1395
+ if err != nil {
1396
+ _ = sendEvent (ctx , codersdk.ServerSentEvent {
1397
+ Type : codersdk .ServerSentEventTypeError ,
1398
+ Data : codersdk.Response {
1399
+ Message : "Internal error converting workspace." ,
1400
+ Detail : err .Error (),
1401
+ },
1402
+ })
1403
+ }
1352
1404
_ = sendEvent (ctx , codersdk.ServerSentEvent {
1353
1405
Type : codersdk .ServerSentEventTypeData ,
1354
- Data : convertWorkspace (
1355
- apiKey .UserID ,
1356
- workspace ,
1357
- data .builds [0 ],
1358
- data .templates [0 ],
1359
- ownerName ,
1360
- api .Options .AllowWorkspaceRenames ,
1361
- ),
1406
+ Data : w ,
1362
1407
})
1363
1408
}
1364
1409
@@ -1505,14 +1550,19 @@ func convertWorkspaces(requesterID uuid.UUID, workspaces []database.Workspace, d
1505
1550
continue
1506
1551
}
1507
1552
1508
- apiWorkspaces = append ( apiWorkspaces , convertWorkspace (
1553
+ w , err := convertWorkspace (
1509
1554
requesterID ,
1510
1555
workspace ,
1511
1556
build ,
1512
1557
template ,
1513
1558
owner .Username ,
1514
1559
data .allowRenames ,
1515
- ))
1560
+ )
1561
+ if err != nil {
1562
+ return nil , xerrors .Errorf ("convert workspace: %w" , err )
1563
+ }
1564
+
1565
+ apiWorkspaces = append (apiWorkspaces , w )
1516
1566
}
1517
1567
return apiWorkspaces , nil
1518
1568
}
@@ -1524,7 +1574,10 @@ func convertWorkspace(
1524
1574
template database.Template ,
1525
1575
ownerName string ,
1526
1576
allowRenames bool ,
1527
- ) codersdk.Workspace {
1577
+ ) (codersdk.Workspace , error ) {
1578
+ if requesterID == uuid .Nil {
1579
+ return codersdk.Workspace {}, xerrors .Errorf ("developer error: requesterID cannot be uuid.Nil!" )
1580
+ }
1528
1581
var autostartSchedule * string
1529
1582
if workspace .AutostartSchedule .Valid {
1530
1583
autostartSchedule = & workspace .AutostartSchedule .String
@@ -1583,7 +1636,7 @@ func convertWorkspace(
1583
1636
AutomaticUpdates : codersdk .AutomaticUpdates (workspace .AutomaticUpdates ),
1584
1637
AllowRenames : allowRenames ,
1585
1638
Favorite : requesterFavorite ,
1586
- }
1639
+ }, nil
1587
1640
}
1588
1641
1589
1642
func convertWorkspaceTTLMillis (i sql.NullInt64 ) * int64 {
0 commit comments