Skip to content

Commit

Permalink
fix: improve errors, log+respond with msg
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnvrm committed Apr 10, 2023
1 parent a3e84ef commit 9bc5a80
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ func (app *App) HandleGChat(w http.ResponseWriter, r *http.Request) {
token := strings.Split(bearerToken, " ")
err := app.cfg.JWKVerifier.VerifyJWT(app.cfg.BotAppID, token[1])
if len(token) != 2 || err != nil {
log.Println("Error verifying JWT: ", err)
http.Error(w, "Unauthorized", http.StatusForbidden)
return
}

message := &gchat.Event{}
err = json.NewDecoder(r.Body).Decode(&message)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Println(err)
log.Println("Error decoding message: ", err)
response := gchat.Response{Text: "Sorry, I couldn't decode your message."}
json.NewEncoder(w).Encode(response)
return
}

Expand Down Expand Up @@ -64,16 +66,16 @@ func (app *App) HandleGChat(w http.ResponseWriter, r *http.Request) {
)

if err != nil {
log.Println(err)
response := gchat.Response{Text: "Sorry, I didn't understand your message."}
log.Println("Error getting response from OpenAI: ", err)
response := gchat.Response{Text: "Sorry, I couldn't communicate your message to OpenAI."}
json.NewEncoder(w).Encode(response)
return
}

out := gchat.Response{Text: response}
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Println(err)
response := gchat.Response{Text: "Sorry, I didn't understand your message."}
log.Println("Error encoding response: ", err)
response := gchat.Response{Text: "Sorry, I couldn't encode the response to you correctly."}
json.NewEncoder(w).Encode(response)
return
}
Expand Down

0 comments on commit 9bc5a80

Please sign in to comment.