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