1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Use content-type to tell unexpected errors from good ones apart.

This commit is contained in:
Sascha L. Teichmann 2022-07-24 16:41:49 +02:00
parent 82feb18eef
commit 7cb376dd0e

View file

@ -15,11 +15,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/ProtonMail/gopenpgp/v2/armor" "github.com/ProtonMail/gopenpgp/v2/armor"
"github.com/ProtonMail/gopenpgp/v2/constants" "github.com/ProtonMail/gopenpgp/v2/constants"
@ -301,6 +303,15 @@ func (p *processor) process(filename string) error {
fmt.Printf("HTTPS %s\n", uploadErr) fmt.Printf("HTTPS %s\n", uploadErr)
} }
// We expect a JSON answer so all other is not valid.
if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
var sb strings.Builder
if _, err := io.Copy(&sb, resp.Body); err != nil {
return fmt.Errorf("reading none JSON reply from server failed: %v", err)
}
return fmt.Errorf("none JSON replay: %v", sb.String())
}
var result struct { var result struct {
Name string `json:"name"` Name string `json:"name"`
ReleaseDate string `json:"release_date"` ReleaseDate string `json:"release_date"`
@ -309,7 +320,6 @@ func (p *processor) process(filename string) error {
} }
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
err := errors.New("Error: view server logs for details")
return err return err
} }