Commit db6535ba authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Merge branch 'errors'

parents 1e57e905 e358a6ee
......@@ -324,7 +324,14 @@ func (session *session) KeyshareBlocked(duration int) {
}
func (session *session) KeyshareError(err error) {
session.fail(&irma.SessionError{ErrorType: irma.ErrorKeyshare, Err: err})
var serr *irma.SessionError
var ok bool
if serr, ok = err.(*irma.SessionError); !ok {
serr = &irma.SessionError{ErrorType: irma.ErrorKeyshare, Err: err}
} else {
serr.ErrorType = irma.ErrorKeyshare
}
session.fail(serr)
}
func (session *session) KeysharePin() {
......
......@@ -3,10 +3,13 @@ package irma
import (
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"strings"
"bytes"
"fmt"
"github.com/go-errors/errors"
)
......@@ -27,8 +30,9 @@ type SessionError struct {
Err error
ErrorType
*ApiError
Info string
Status int
Info string
Status int
StatusMessage string
}
// ApiError is an error message returned by the API server on errors.
......@@ -40,6 +44,14 @@ type ApiError struct {
Stacktrace string `json:"stacktrace"`
}
func (err *ApiError) Error() string {
var msg string
if err.Message != "" {
msg = fmt.Sprintf(" (%s)", err.Message)
}
return fmt.Sprintf("%s%s: %s", err.ErrorName, msg, err.Description)
}
// Qr contains the data of an IRMA session QR (as generated by irma_js),
// suitable for NewSession().
type Qr struct {
......@@ -110,10 +122,28 @@ const (
)
func (e *SessionError) Error() string {
var buffer bytes.Buffer
typ := e.ErrorType
if typ == "" {
typ = ErrorType("unknown")
}
buffer.WriteString("Error type: ")
buffer.WriteString(string(typ))
if e.Err != nil {
return fmt.Sprintf("%s: %s", string(e.ErrorType), e.Err.Error())
buffer.WriteString("\nDescription: ")
buffer.WriteString(e.Err.Error())
}
return string(e.ErrorType)
if e.Status != 200 && e.StatusMessage != "" {
buffer.WriteString("\nConnection: ")
buffer.WriteString(e.StatusMessage)
}
if e.ApiError != nil {
buffer.WriteString("\nIRMA server error: ")
buffer.WriteString(e.ApiError.Error())
}
return buffer.String()
}
func JwtDecode(jwt string, body interface{}) error {
......
......@@ -110,18 +110,18 @@ func (transport *HTTPTransport) jsonRequest(url string, method string, result in
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode}
return &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode, StatusMessage: res.Status}
}
if res.StatusCode != 200 {
apierr := &ApiError{}
err = json.Unmarshal(body, apierr)
if err != nil || apierr.ErrorName == "" { // Not an ApiErrorMessage
return &SessionError{ErrorType: ErrorServerResponse, Status: res.StatusCode}
return &SessionError{ErrorType: ErrorServerResponse, Status: res.StatusCode, StatusMessage: res.Status}
}
if verbose {
fmt.Printf("ERROR: %+v\n", apierr)
}
return &SessionError{ErrorType: ErrorApi, Status: res.StatusCode, ApiError: apierr}
return &SessionError{ErrorType: ErrorApi, Status: res.StatusCode, ApiError: apierr, StatusMessage: res.Status}
}
if verbose {
......@@ -132,7 +132,7 @@ func (transport *HTTPTransport) jsonRequest(url string, method string, result in
} else {
err = json.Unmarshal(body, result)
if err != nil {
return &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode}
return &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode, StatusMessage: res.Status}
}
}
......@@ -150,7 +150,7 @@ func (transport *HTTPTransport) GetBytes(url string) ([]byte, error) {
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode}
return nil, &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode, StatusMessage: res.Status}
}
return b, nil
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment