diff --git a/http/mdm/mdm.go b/http/mdm/mdm.go index 0325280..e09e2ef 100644 --- a/http/mdm/mdm.go +++ b/http/mdm/mdm.go @@ -37,12 +37,19 @@ 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 } + logs = append(logs, "http_status", httpStatus) + // 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)) + } + logger.Info(logs...) http.Error(w, http.StatusText(httpStatus), httpStatus) } w.Write(respBytes) @@ -61,12 +68,18 @@ 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", "err", err} httpStatus := http.StatusInternalServerError var statusErr *service.HTTPStatusError if errors.As(err, &statusErr) { httpStatus = statusErr.Status } + logs = append(logs, "http_status", httpStatus) + var parseErr *mdm.ParseError + if errors.As(err, &parseErr) { + logs = append(logs, "content", string(parseErr.Content)) + } + logger.Info(logs...) http.Error(w, http.StatusText(httpStatus), httpStatus) } w.Write(respBytes) diff --git a/mdm/checkin.go b/mdm/checkin.go index 8f4e6ff..db9a38a 100644 --- a/mdm/checkin.go +++ b/mdm/checkin.go @@ -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 diff --git a/mdm/command.go b/mdm/command.go index c24fbd0..3be9998 100644 --- a/mdm/command.go +++ b/mdm/command.go @@ -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 == "" { @@ -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 == "" { diff --git a/mdm/mdm.go b/mdm/mdm.go index 23c62b7..120871a 100644 --- a/mdm/mdm.go +++ b/mdm/mdm.go @@ -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)) }