types.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains custom types, currently only a timestamp wrapper.
  7. package discordgo
  8. import (
  9. "encoding/json"
  10. "net/http"
  11. )
  12. // RESTError stores error information about a request with a bad response code.
  13. // Message is not always present, there are cases where api calls can fail
  14. // without returning a json message.
  15. type RESTError struct {
  16. Request *http.Request
  17. Response *http.Response
  18. ResponseBody []byte
  19. Message *APIErrorMessage // Message may be nil.
  20. }
  21. func newRestError(req *http.Request, resp *http.Response, body []byte) *RESTError {
  22. restErr := &RESTError{
  23. Request: req,
  24. Response: resp,
  25. ResponseBody: body,
  26. }
  27. // Attempt to decode the error and assume no message was provided if it fails
  28. var msg *APIErrorMessage
  29. err := json.Unmarshal(body, &msg)
  30. if err == nil {
  31. restErr.Message = msg
  32. }
  33. return restErr
  34. }
  35. func (r RESTError) Error() string {
  36. return "HTTP " + r.Response.Status + ", " + string(r.ResponseBody)
  37. }