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