Skip to content

Commit

Permalink
Improve qr-code example
Browse files Browse the repository at this point in the history
- Improve error message when query param is missing
  • Loading branch information
philippgille committed Sep 5, 2018
1 parent 714793f commit 2b0872b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions examples/qr-code/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ func main() {

func qrHandler(c *gin.Context) {
data := c.Query("data")
qrBytes, err := qrcode.Encode(data, qrcode.Medium, 256)
if err != nil {
c.String(http.StatusInternalServerError, "There was an error encoding the data as QR code")
if data == "" {
c.String(http.StatusBadRequest, "The query parameter \"data\" is missing")
c.Abort()
} else {
c.Data(http.StatusOK, "image/png", qrBytes)
qrBytes, err := qrcode.Encode(data, qrcode.Medium, 256)
if err != nil {
c.String(http.StatusInternalServerError, "There was an error encoding the data as QR code")
c.Abort()
} else {
c.Data(http.StatusOK, "image/png", qrBytes)
}
}
}

0 comments on commit 2b0872b

Please sign in to comment.