@@ -1448,11 +1448,19 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
1448
1448
UserVariableValues : req .UserVariableValues ,
1449
1449
})
1450
1450
if err != nil {
1451
- return xerrors .Errorf ("marshal job input: %w" , err )
1451
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1452
+ Message : "Internal error creating template version." ,
1453
+ Detail : xerrors .Errorf ("marshal job input: %w" , err ).Error (),
1454
+ })
1455
+ return err
1452
1456
}
1453
1457
traceMetadataRaw , err := json .Marshal (tracing .MetadataFromContext (ctx ))
1454
1458
if err != nil {
1455
- return xerrors .Errorf ("marshal job metadata: %w" , err )
1459
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1460
+ Message : "Internal error creating template version." ,
1461
+ Detail : xerrors .Errorf ("marshal job metadata: %w" , err ).Error (),
1462
+ })
1463
+ return err
1456
1464
}
1457
1465
1458
1466
provisionerJob , err = tx .InsertProvisionerJob (ctx , database.InsertProvisionerJobParams {
@@ -1473,7 +1481,11 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
1473
1481
},
1474
1482
})
1475
1483
if err != nil {
1476
- return xerrors .Errorf ("insert provisioner job: %w" , err )
1484
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1485
+ Message : "Internal error creating template version." ,
1486
+ Detail : xerrors .Errorf ("insert provisioner job: %w" , err ).Error (),
1487
+ })
1488
+ return err
1477
1489
}
1478
1490
1479
1491
var templateID uuid.NullUUID
@@ -1501,20 +1513,36 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
1501
1513
CreatedBy : apiKey .UserID ,
1502
1514
})
1503
1515
if err != nil {
1504
- return xerrors .Errorf ("insert template version: %w" , err )
1516
+ if database .IsUniqueViolation (err , database .UniqueTemplateVersionsTemplateIDNameKey ) {
1517
+ httpapi .Write (ctx , rw , http .StatusConflict , codersdk.Response {
1518
+ Message : fmt .Sprintf ("A template version with name %q already exists for this template." , req .Name ),
1519
+ Validations : []codersdk.ValidationError {{
1520
+ Field : "name" ,
1521
+ Detail : "This value is already in use and should be unique." ,
1522
+ }},
1523
+ })
1524
+ return err
1525
+ }
1526
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1527
+ Message : "Internal error creating template version." ,
1528
+ Detail : xerrors .Errorf ("insert template version: %w" , err ).Error (),
1529
+ })
1530
+ return err
1505
1531
}
1506
1532
1507
1533
templateVersion , err = tx .GetTemplateVersionByID (ctx , templateVersionID )
1508
1534
if err != nil {
1509
- return xerrors .Errorf ("fetched inserted template version: %w" , err )
1535
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1536
+ Message : "Internal error creating template version." ,
1537
+ Detail : xerrors .Errorf ("fetched inserted template version: %w" , err ).Error (),
1538
+ })
1539
+ return err
1510
1540
}
1511
1541
1512
1542
return nil
1513
1543
}, nil )
1514
1544
if err != nil {
1515
- httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1516
- Message : err .Error (),
1517
- })
1545
+ // Each failure case in the tx should have already written a response.
1518
1546
return
1519
1547
}
1520
1548
aReq .New = templateVersion
0 commit comments