handle json parsing errors

This commit is contained in:
Hekmon 2022-04-29 11:39:11 +02:00
parent 353f425bbf
commit aa3f8e1b34
No known key found for this signature in database
GPG key ID: 8C27AA76AB6EFF96

View file

@ -76,7 +76,9 @@ func LoginWithCredentials(user string, password string, locale LOCALE, client *h
var data map[string]interface{} var data map[string]interface{}
body, _ := io.ReadAll(sessResp.Body) body, _ := io.ReadAll(sessResp.Body)
json.Unmarshal(body, &data) if err = json.Unmarshal(body, &data); err != nil {
return nil, fmt.Errorf("failed to parse start session with credentials response: %w", err)
}
sessionID := data["data"].(map[string]interface{})["session_id"].(string) sessionID := data["data"].(map[string]interface{})["session_id"].(string)
@ -126,7 +128,9 @@ func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*
return nil, fmt.Errorf("failed to start session: %s", resp.Status) return nil, fmt.Errorf("failed to start session: %s", resp.Status)
} }
json.NewDecoder(resp.Body).Decode(&jsonBody) if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, fmt.Errorf("failed to parse start session with session id response: %w", err)
}
if _, ok := jsonBody["message"]; ok { if _, ok := jsonBody["message"]; ok {
return nil, errors.New("invalid session id") return nil, errors.New("invalid session id")
} }
@ -178,7 +182,9 @@ func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&jsonBody) if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, fmt.Errorf("failed to parse 'token' response: %w", err)
}
crunchy.Config.TokenType = jsonBody["token_type"].(string) crunchy.Config.TokenType = jsonBody["token_type"].(string)
crunchy.Config.AccessToken = jsonBody["access_token"].(string) crunchy.Config.AccessToken = jsonBody["access_token"].(string)
@ -189,7 +195,9 @@ func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&jsonBody) if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, fmt.Errorf("failed to parse 'index' response: %w", err)
}
cms := jsonBody["cms"].(map[string]interface{}) cms := jsonBody["cms"].(map[string]interface{})
crunchy.Config.Policy = cms["policy"].(string) crunchy.Config.Policy = cms["policy"].(string)
@ -203,7 +211,9 @@ func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&jsonBody) if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, fmt.Errorf("failed to parse 'me' response: %w", err)
}
crunchy.Config.AccountID = jsonBody["account_id"].(string) crunchy.Config.AccountID = jsonBody["account_id"].(string)
crunchy.Config.ExternalID = jsonBody["external_id"].(string) crunchy.Config.ExternalID = jsonBody["external_id"].(string)
@ -215,7 +225,9 @@ func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&jsonBody) if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, fmt.Errorf("failed to parse 'profile' response: %w", err)
}
crunchy.Config.MaturityRating = jsonBody["maturity_rating"].(string) crunchy.Config.MaturityRating = jsonBody["maturity_rating"].(string)