httpError - Set detail string to the content of the HTTP response if it's not parsed as JSON

Fixes #188
This commit is contained in:
xenolf 2016-04-15 03:09:29 +02:00
parent cbca761215
commit 094e3d41bb

View file

@ -3,6 +3,7 @@ package acme
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
@ -53,13 +54,21 @@ func (c challengeError) Error() string {
func handleHTTPError(resp *http.Response) error {
var errorDetail RemoteError
contenType := resp.Header.Get("Content-Type")
// try to decode the content as JSON
if resp.Header.Get("Content-Type") == "application/json" {
if contenType == "application/json" || contenType == "application/problem+json" {
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&errorDetail)
if err != nil {
return err
}
} else {
detailBytes, err := ioutil.ReadAll(limitReader(resp.Body, 1024*1024))
if err != nil {
return err
}
errorDetail.Detail = string(detailBytes)
}
errorDetail.StatusCode = resp.StatusCode