Skip to content

Commit

Permalink
Merge pull request #318 from Cull-Methi/better-errors
Browse files Browse the repository at this point in the history
Add better error checking around 404s and actual database errors
  • Loading branch information
zhouzhuojie authored Mar 19, 2020
2 parents b592f47 + b390b52 commit 0febaaf
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/handler/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,18 @@ func (c *crud) CreateFlag(params flag.CreateFlagParams) middleware.Responder {

func (c *crud) GetFlag(params flag.GetFlagParams) middleware.Responder {
f := &entity.Flag{}
err := entity.PreloadSegmentsVariants(getDB()).First(f, params.FlagID).Error
if err != nil {
result := entity.PreloadSegmentsVariants(getDB()).First(f, params.FlagID)

// Flag with given ID doesn't exist, so we 404
if result.RecordNotFound() {
return flag.NewGetFlagDefault(404).WithPayload(
ErrorMessage("cannot find flag %v. %s", params.FlagID, err))
ErrorMessage("unable to find flag %v in the database", params.FlagID))
}

// Something else happened, return a 500
if err := result.Error; err != nil {
return flag.NewGetFlagDefault(500).WithPayload(
ErrorMessage("an unknown error occurred while looking up flag %v: %s", params.FlagID, err))
}

resp := flag.NewGetFlagOK()
Expand Down

0 comments on commit 0febaaf

Please sign in to comment.