func (e *fancyDecodeError) Error() string {
	mediaType, _, err := mime.ParseMediaType(e.contentType)
	if err != nil {
		// Even if there's no media type, we want to see something useful.
		mediaType = fmt.Sprintf("%q", e.contentType)
	}

	// TODO use charset.NewReader to convert from non-utf8 content?
	switch mediaType {
	case "text/html":
		text, err := htmlToText(bytes.NewReader(e.body))
		if err != nil {
			// Note: it seems that this can never actually
			// happen - the only way that the HTML parser
			// can fail is if there's a read error and we've
			// removed that possibility by using
			// noErrorReader above.
			return fmt.Sprintf("unexpected (and invalid) content text/html; want application/json; content: %q", sizeLimit(e.body))
		}
		if len(text) == 0 {
			return fmt.Sprintf(`unexpected content type text/html; want application/json; content: %q`, sizeLimit(e.body))
		}
		return fmt.Sprintf(`unexpected content type text/html; want application/json; content: %s`, sizeLimit(text))
	case "text/plain":
		return fmt.Sprintf(`unexpected content type text/plain; want application/json; content: %s`, sizeLimit(sanitizeText(string(e.body), true)))
	default:
		return fmt.Sprintf(`unexpected content type %s; want application/json; content: %q`, mediaType, sizeLimit(e.body))
	}
}
// Error implements error.Error by trying to produce a decent
// error message derived from the body content.
Error implements error.Error by trying to produce a decent
error message derived from the body content.
// Error implements error . Error by trying to produce a decent // error message derived from the body content .
func (e *fancyDecodeError) Error() string {
	mediaType, _, err := mime.ParseMediaType(e.contentType)
	if err != nil {
		mediaType = fmt.Sprintf("%q", e.contentType)
	}
	switch mediaType {
	case "text/html":
		text, err := htmlToText(bytes.NewReader(e.body))
		if err != nil {
			return fmt.Sprintf("unexpected (and invalid) content text/html; want application/json; content: %q", sizeLimit(e.body))
		}
		if len(text) == 0 {
			return fmt.Sprintf(`unexpected content type text/html; want application/json; content: %q`, sizeLimit(e.body))
		}
		return fmt.Sprintf(`unexpected content type text/html; want application/json; content: %s`, sizeLimit(text))
	case "text/plain":
		return fmt.Sprintf(`unexpected content type text/plain; want application/json; content: %s`, sizeLimit(sanitizeText(string(e.body), true)))
	default:
		return fmt.Sprintf(`unexpected content type %s; want application/json; content: %q`, mediaType, sizeLimit(e.body))
	}
}
func ( e * fancyDecodeError ) Error ( ) string { mediaType , _ , err := mime . ParseMediaType ( e . contentType ) if err != nil { mediaType = fmt . Sprintf ( " " , e . contentType ) } switch mediaType { case " " : text , err := htmlToText ( bytes . NewReader ( e . body ) ) if err != nil { return fmt . Sprintf ( " " , sizeLimit ( e . body ) ) } if len ( text ) == 0 { return fmt . Sprintf ( `unexpected content type text/html; want application/json; content: %q` , sizeLimit ( e . body ) ) } return fmt . Sprintf ( `unexpected content type text/html; want application/json; content: %s` , sizeLimit ( text ) ) case " " : return fmt . Sprintf ( `unexpected content type text/plain; want application/json; content: %s` , sizeLimit ( sanitizeText ( string ( e . body ) , true ) ) ) default : return fmt . Sprintf ( `unexpected content type %s; want application/json; content: %q` , mediaType , sizeLimit ( e . body ) ) } }
Error implements error.Error by trying to produce a decent
error message derived from the body content.
Error implements error . Error by trying to produce a decent error message derived from the body content .
// Even if there's no media type, we want to see something useful. // TODO use charset.NewReader to convert from non-utf8 content? // Note: it seems that this can never actually // happen - the only way that the HTML parser // can fail is if there's a read error and we've // removed that possibility by using // noErrorReader above.