Fiat: improve error handling

This commit is contained in:
andig 2022-03-13 12:21:30 +00:00
parent d37c8840e1
commit ac45abeea8
2 changed files with 21 additions and 3 deletions

View file

@ -70,7 +70,7 @@ func (v *Identity) Login() error {
}
var res struct {
ErrorCode int
ErrorInfo
UID string
StatusReason string
SessionInfo struct {
@ -107,13 +107,14 @@ func (v *Identity) Login() error {
if req, err = request.New(http.MethodPost, uri, strings.NewReader(data.Encode()), headers); err == nil {
if err = v.DoJSON(req, &res); err == nil {
err = res.ErrorInfo.Error()
v.uid = res.UID
}
}
}
var token struct {
ErrorCode int `json:"errorCode"`
ErrorInfo
StatusReason string
IDToken string `json:"id_token"`
}
@ -138,7 +139,9 @@ func (v *Identity) Login() error {
if req, err = request.New(http.MethodGet, uri, nil, headers); err == nil {
req.URL.RawQuery = data.Encode()
err = v.DoJSON(req, &token)
if err = v.DoJSON(req, &token); err == nil {
err = token.ErrorInfo.Error()
}
}
}

View file

@ -1,10 +1,25 @@
package fiat
import (
"fmt"
"html"
"strconv"
"time"
)
type ErrorInfo struct {
ErrorCode int
ErrorMessage string
ErrorDetails string
}
func (e ErrorInfo) Error() error {
if e.ErrorCode == 0 {
return nil
}
return fmt.Errorf("%s: %s", e.ErrorMessage, html.UnescapeString(e.ErrorDetails))
}
type VehiclesResponse struct {
Vehicles []Vehicle
}