Skip to content

Commit

Permalink
Use Content for ParseError and log separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Lee committed Jul 26, 2022
1 parent 5dc2c16 commit 61867b1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
21 changes: 19 additions & 2 deletions http/mdm/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ func CheckinHandler(svc service.Checkin, logger log.Logger) http.HandlerFunc {
}
respBytes, err := service.CheckinRequest(svc, mdmReqFromHTTPReq(r), bodyBytes)
if err != nil {
logger.Info("msg", "check-in request", "err", err)
logs := []interface{}{"msg", "check-in request", "err", err}
httpStatus := http.StatusInternalServerError
var statusErr *service.HTTPStatusError
if errors.As(err, &statusErr) {
httpStatus = statusErr.Status
err = statusErr.Unwrap()
}
// manualy unwrapping the `StatusErr` is not necessary as `errors.As` manually unwraps
var parseErr *mdm.ParseError
if errors.As(err, &parseErr) {
logs = append(logs, "content", string(parseErr.Content))
err = statusErr.Unwrap()
}
logs = append(logs, "http_status", httpStatus, "err", err)
logger.Info(logs...)
http.Error(w, http.StatusText(httpStatus), httpStatus)
}
w.Write(respBytes)
Expand All @@ -61,12 +70,20 @@ func CommandAndReportResultsHandler(svc service.CommandAndReportResults, logger
}
respBytes, err := service.CommandAndReportResultsRequest(svc, mdmReqFromHTTPReq(r), bodyBytes)
if err != nil {
logger.Info("msg", "command report results", "err", err)
logs := []interface{}{"msg", "command report results"}
httpStatus := http.StatusInternalServerError
var statusErr *service.HTTPStatusError
if errors.As(err, &statusErr) {
httpStatus = statusErr.Status
err = statusErr.Unwrap()
}
var parseErr *mdm.ParseError
if errors.As(err, &parseErr) {
logs = append(logs, "content", string(parseErr.Content))
err = parseErr.Unwrap()
}
logs = append(logs, "http_status", httpStatus, "err", err)
logger.Info(logs...)
http.Error(w, http.StatusText(httpStatus), httpStatus)
}
w.Write(respBytes)
Expand Down
2 changes: 1 addition & 1 deletion mdm/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func DecodeCheckin(rawMessage []byte) (message interface{}, err error) {
w := &checkinUnmarshaller{raw: rawMessage}
err = plist.Unmarshal(rawMessage, w)
if err != nil {
err = &ParseError{Err: err, Body: rawMessage}
err = &ParseError{Err: err, Content: rawMessage}
}
message = w.message
return
Expand Down
4 changes: 2 additions & 2 deletions mdm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func DecodeCommandResults(rawResults []byte) (results *CommandResults, err error
results = new(CommandResults)
err = plist.Unmarshal(rawResults, results)
if err != nil {
return nil, &ParseError{Err: err, Body: rawResults}
return nil, &ParseError{Err: err, Content: rawResults}
}
results.Raw = rawResults
if results.Status == "" {
Expand All @@ -58,7 +58,7 @@ func DecodeCommand(rawCommand []byte) (command *Command, err error) {
command = new(Command)
err = plist.Unmarshal(rawCommand, command)
if err != nil {
return nil, &ParseError{Err: err, Body: rawCommand}
return nil, &ParseError{Err: err, Content: rawCommand}
}
command.Raw = rawCommand
if command.CommandUUID == "" || command.Command.RequestType == "" {
Expand Down
7 changes: 5 additions & 2 deletions mdm/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ func (r *Request) Clone() *Request {
return r2
}

// ParseError represents a failure to parse an MDM structure (usually Apple Plist)
type ParseError struct {
Err error
Body []byte
Content []byte
}

// Unwrap returns the underlying error of the ParseError
func (e *ParseError) Unwrap() error {
return e.Err
}

// Error formats the ParseError as a string
func (e *ParseError) Error() string {
return fmt.Sprintf("parse error: %s: raw body: %v", e.Err, string(e.Body))
return fmt.Sprintf("parse error: %v: raw content: %v", e.Err, string(e.Content))
}

0 comments on commit 61867b1

Please sign in to comment.