handle error messages
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					api_test.go
 | 
				
			||||||
							
								
								
									
										4
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								api.go
									
									
									
									
									
								
							@ -49,6 +49,10 @@ func request[T any](api *Api, query string, vars map[string]interface{}, respObj
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer resp.Body.Close()
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if resp.StatusCode != 200 {
 | 
				
			||||||
 | 
							return handleError(resp)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//data, _ := ioutil.ReadAll(resp.Body)
 | 
						//data, _ := ioutil.ReadAll(resp.Body)
 | 
				
			||||||
	//fmt.Println(string(data))
 | 
						//fmt.Println(string(data))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										32
									
								
								api_error.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								api_error.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package anilist
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type apiError struct {
 | 
				
			||||||
 | 
						Message string `json:"message"`
 | 
				
			||||||
 | 
						Status  int    `json:"status"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type apiErrResp struct {
 | 
				
			||||||
 | 
						Errors []apiError `json:"errors"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func handleError(resp *http.Response) error {
 | 
				
			||||||
 | 
						respObj := &apiErrResp{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dec := json.NewDecoder(resp.Body)
 | 
				
			||||||
 | 
						err := dec.Decode(respObj)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(respObj.Errors) == 0 {
 | 
				
			||||||
 | 
							return errors.New("unknown API error")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return errors.New(respObj.Errors[0].Message)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user