Better error handling in list API
This commit is contained in:
@@ -4,11 +4,17 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"fornaxian.com/pixeldrain-web/conf"
|
||||
"fornaxian.com/pixeldrain-web/log"
|
||||
)
|
||||
|
||||
// API error constants
|
||||
const (
|
||||
ListNotFoundError = "list_not_found"
|
||||
)
|
||||
|
||||
// List information object from the pixeldrain API
|
||||
type List struct {
|
||||
Error *ErrorResponse
|
||||
Success bool `json:"success"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
DateCreated int64 `json:"date_created"`
|
||||
@@ -26,19 +32,24 @@ type ListFile struct {
|
||||
ListDescription string `json:"list_description"`
|
||||
}
|
||||
|
||||
// GetList get a List from the pixeldrain API
|
||||
// GetList get a List from the pixeldrain API. Errors will be available through
|
||||
// List.Error. Standard error checks apply.
|
||||
func GetList(id string) *List {
|
||||
var list = &List{}
|
||||
body, err := getString(conf.ApiUrlInternal() + "/list/" + id)
|
||||
if err != nil {
|
||||
list.Error = errorResponseFromError(err)
|
||||
return list
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(body), list)
|
||||
if err != nil {
|
||||
log.Error("req failed: %v", err)
|
||||
return nil
|
||||
list.Error = errorResponseFromError(err)
|
||||
return list
|
||||
}
|
||||
var list List
|
||||
err = json.Unmarshal([]byte(body), &list)
|
||||
if err != nil {
|
||||
log.Error("unmarshal failed: %v. json: %s", err, body)
|
||||
return nil
|
||||
|
||||
if !list.Success {
|
||||
list.Error = errorResponseFromJSON(body)
|
||||
}
|
||||
return &list
|
||||
return list
|
||||
}
|
||||
|
@@ -1,8 +1,37 @@
|
||||
package pixelapi
|
||||
|
||||
import "net/http"
|
||||
import "io/ioutil"
|
||||
import "io"
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrorResponse struct {
|
||||
ReqError bool
|
||||
Success bool `json:"success"`
|
||||
Value string `json:"value"`
|
||||
Message *string `json:"message"`
|
||||
ID *string `json:"id"`
|
||||
}
|
||||
|
||||
func errorResponseFromJSON(j string) *ErrorResponse {
|
||||
var r = &ErrorResponse{}
|
||||
var err = json.Unmarshal([]byte(j), r)
|
||||
if err != nil {
|
||||
r.Success = false
|
||||
r.ReqError = true
|
||||
r.Value = err.Error()
|
||||
}
|
||||
return r
|
||||
}
|
||||
func errorResponseFromError(e error) *ErrorResponse {
|
||||
var r = &ErrorResponse{}
|
||||
r.Success = false
|
||||
r.ReqError = true
|
||||
r.Value = e.Error()
|
||||
return r
|
||||
}
|
||||
|
||||
func getString(url string) (string, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
|
@@ -13,13 +13,16 @@ import (
|
||||
// ServeListViewer controller for GET /l/:id
|
||||
func ServeListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
var list = pixelapi.GetList(p.ByName("id"))
|
||||
if list == nil {
|
||||
if list.Error != nil {
|
||||
if list.Error.ReqError {
|
||||
log.Error("API request error occurred: %s", list.Error.Value)
|
||||
}
|
||||
ServeNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var ogData OGData
|
||||
var err error
|
||||
var ogData OGData
|
||||
listdata := map[string]interface{}{
|
||||
"id": list.ID,
|
||||
"data": list.Files,
|
||||
|
Reference in New Issue
Block a user