error handling

This commit is contained in:
Michael 2019-06-20 01:33:48 +01:00
parent cb112d9961
commit c941404d7c
2 changed files with 81 additions and 22 deletions

81
api.go
View File

@ -2,6 +2,8 @@ package request
import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
@ -11,43 +13,61 @@ import (
"github.com/fatih/structs"
)
type HTTPError struct {
Status string
StatusCode int
}
func url(path string) string {
return string("https://forum.0cd.xyz/" + path)
}
// Request sends GET to path
func Request(path string) []byte {
func Request(path string) ([]byte, *HTTPError) {
req, err := http.NewRequest("GET", url(path), nil)
if err != nil {
log.Fatal("Error reading request. ", err)
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
for _, headers := range Header().Headers {
head, er := Header()
if er != nil {
return nil, er
}
for _, headers := range head.Headers {
req.Header.Set(headers.Name, headers.Value)
}
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error reading request. ", err)
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading request. ", err)
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return body
return body, nil
}
return nil
fmt.Printf("Request Error: GET %d %s\n", resp.StatusCode, html.EscapeString(url(path)))
return nil, &HTTPError{Status: resp.Status, StatusCode: resp.StatusCode}
}
// Category returns category json data
func Category() map[string]interface{} {
var category Categories
json.Unmarshal(Request("categories"), &category)
resp, err := Request("categories")
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &category)
m := structs.Map(category)
return m
}
@ -55,7 +75,12 @@ func Category() map[string]interface{} {
// Category returns category json data
func CategoryTopic(path string) map[string]interface{} {
var topics CategoryTopics
json.Unmarshal(Request("/c/"+path), &topics)
resp, err := Request("/c/" + path)
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &topics)
m := structs.Map(topics)
return m
}
@ -63,7 +88,12 @@ func CategoryTopic(path string) map[string]interface{} {
// About gets json data from about page
func About() map[string]interface{} {
var about AutoGen
json.Unmarshal(Request("about"), &about)
resp, err := Request("about")
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &about)
for i := 0; i < len(about.About.Admins); i++ {
about.About.Admins[i].AvatarTemplate = strings.ReplaceAll(about.About.Admins[i].AvatarTemplate, "{size}", "120")
}
@ -74,23 +104,42 @@ func About() map[string]interface{} {
// Tag returns tags json data
func Tag() map[string]interface{} {
var tags Tags
json.Unmarshal(Request("tags"), &tags)
resp, err := Request("tags")
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &tags)
m := structs.Map(tags)
return m
}
// GetTopics gets topic list from tag
func GetTopics(path string) (topics TagTopics) {
json.Unmarshal(Request("/tags/"+path), &topics)
func GetTopics(path string) (topics TagTopics, err *HTTPError) {
resp, err := Request("tags/" + path)
if err != nil {
return
}
json.Unmarshal(resp, &topics)
return
}
// Topics n/a
func Topics(path string) map[string]interface{} {
var topic TopicsList
for _, topics := range GetTopics(path).TopicList.Topics {
resp, err := GetTopics(path)
if err != nil {
m := structs.Map(err)
return m
}
for _, topics := range resp.TopicList.Topics {
var t Topic
json.Unmarshal(Request("/t/"+topics.Slug), &t)
resp, err := Request("/t/" + topics.Slug)
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &t)
for i := 0; i < len(t.PostStream.Posts); i++ {
if t.PostStream.Posts[i].PostNumber != 1 {
t.PostStream.Posts[i].Cooked = ""

22
file.go
View File

@ -10,24 +10,34 @@ import (
)
// File reads request json file
func File(path string) (b []byte) {
func File(path string) ([]byte, *HTTPError) {
jsonFile, err := os.Open(path)
if err != nil {
log.Fatal("Error reading request. ", err)
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
return byteValue
return byteValue, nil
}
func Contact() map[string]interface{} {
var contact Contacts
json.Unmarshal(File("./json/contacts.json"), &contact)
resp, err := File("./json/contacts.json")
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &contact)
m := structs.Map(contact)
return m
}
func Header() (headers Headers) {
json.Unmarshal(File("./json/headers.json"), &headers)
func Header() (headers *Headers, err *HTTPError) {
resp, err := File("./json/headers.json")
if err != nil {
return
}
json.Unmarshal(resp, &headers)
return
}