magistrala icon indicating copy to clipboard operation
magistrala copied to clipboard

Bug: Log doesn't have full errors, instead it returns limited error information

Open arvindh123 opened this issue 1 year ago • 2 comments

What were you trying to achieve?

I'm trying to simulate on database rollback error for a service,

What are the expected results?

Logs with rollback error ( log with all nested errors)

What are the received results?

Logs without rollback error ( log with top two layer errors)

Steps To Reproduce

Modified source code to throw rollback error in any service for create entity

In what environment did you encounter the issue?

Docker

Additional information you deem important

We should replace args = append(args, slog.Any("error", err)) with args = append(args, slog.String("error", err.Error()))

Because slog.Any("error", err) use JSON Marshal to stringify the error and We have custom JSON Marshal for Magistrala errors https://github.com/absmach/magistrala/blob/main/pkg/errors/errors.go#L59-L71, So in logs we could not get full error .

func (ce *customError) MarshalJSON() ([]byte, error) {
	var val string
	if e := ce.Err(); e != nil {
		val = e.Msg()
	}
	return json.Marshal(&struct {
		Err string `json:"error"`
		Msg string `json:"message"`
	}{
		Err: val,
		Msg: ce.Msg(),
	})
}

arvindh123 avatar May 28 '24 09:05 arvindh123