Skip to content

Commit

Permalink
use IPPError in Response CheckForError method
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian committed Mar 27, 2020
1 parent d2f1f7d commit a88a2fa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
3 changes: 3 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ipp

import "fmt"

// check a given error whether a printer or class does not exist
func IsNotExistsError(err error) bool {
if err == nil {
return false
Expand All @@ -10,6 +11,7 @@ func IsNotExistsError(err error) bool {
return err.Error() == "The printer or class does not exist."
}

//non ok ipp status codes
type IPPError struct {
Status int16
Message string
Expand All @@ -19,6 +21,7 @@ func (e IPPError) Error() string {
return fmt.Sprintf("ipp status: %d, message: %s", e.Status, e.Message)
}

// non 200 http codes
type HTTPError struct {
Code int
}
Expand Down
15 changes: 2 additions & 13 deletions ipp-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,8 @@ func (c *IPPClient) SendRequest(url string, req *Request, additionalResponseData
return nil, err
}

if resp.StatusCode == StatusOk {
return resp, nil
}

msg := ""
if statusMessage, ok := resp.OperationAttributes[AttributeStatusMessage]; ok {
msg = statusMessage[0].Value.(string)
}

return resp, IPPError{
Status: resp.StatusCode,
Message: msg,
}
err = resp.CheckForErrors()
return resp, err
}

// Print one or more `Document`s using IPP `Create-Job` followed by `Send-Document` request(s).
Expand Down
14 changes: 9 additions & 5 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)

Expand All @@ -23,12 +22,17 @@ type Response struct {
}

func (r *Response) CheckForErrors() error {
if r.StatusCode != 0 {
if len(r.OperationAttributes["status-message"]) == 0 {
return fmt.Errorf("ipp server return error code %d but no status message", r.StatusCode)
if r.StatusCode != StatusOk {
err := IPPError{
Status: r.StatusCode,
Message: "no status message returned",
}

return errors.New(r.OperationAttributes["status-message"][0].Value.(string))
if len(r.OperationAttributes["status-message"]) > 0 {
err.Message = r.OperationAttributes["status-message"][0].Value.(string)
}

return err
}

return nil
Expand Down

0 comments on commit a88a2fa

Please sign in to comment.