Skip to content

Commit

Permalink
fix: nil panics
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas committed Jul 8, 2024
1 parent cb0766f commit afbcfb2
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 31 deletions.
11 changes: 0 additions & 11 deletions selfservice/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,3 @@ type Flow interface {
type FlowWithRedirect interface {
SecureRedirectToOpts(ctx context.Context, cfg config.Provider) (opts []x.SecureRedirectOption)
}

type FlowWithToLoggerField interface {
ToLoggerField() map[string]interface{}
}

func ToLoggerField(f FlowWithToLoggerField) map[string]interface{} {
if f == nil {
return nil
}
return f.ToLoggerField()
}
11 changes: 8 additions & 3 deletions selfservice/flow/login/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ func (s *ErrorHandler) PrepareReplacementForExpiredFlow(w http.ResponseWriter, r
}

func (s *ErrorHandler) WriteFlowError(w http.ResponseWriter, r *http.Request, f *Flow, group node.UiNodeGroup, err error) {
s.d.Audit().
logger := s.d.Audit().
WithError(err).
WithRequest(r).
WithField("login_flow", flow.ToLoggerField(f)).
WithRequest(r)

if f != nil {
logger = logger.WithField("login_flow", f.ToLoggerField())
}

logger.
Info("Encountered self-service login error.")

if f == nil {
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/login/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (f *Flow) SetReturnToVerification(to string) {
f.ReturnToVerification = to
}

func (f *Flow) ToLoggerField() map[string]interface{} {
func (f Flow) ToLoggerField() map[string]interface{} {
return map[string]interface{}{
"id": f.ID.String(),
"return_to": f.ReturnTo,
Expand Down
9 changes: 6 additions & 3 deletions selfservice/flow/recovery/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ func (s *ErrorHandler) WriteFlowError(
group node.UiNodeGroup,
recoveryErr error,
) {
s.d.Audit().
logger := s.d.Audit().
WithError(recoveryErr).
WithRequest(r).
WithField("recovery_flow", flow.ToLoggerField(f)).
WithRequest(r)
if f != nil {
logger = logger.WithField("recovery_flow", f.ToLoggerField())
}
logger.
Info("Encountered self-service recovery error.")

if f == nil {
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/recovery/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (t *Flow) GetTransientPayload() json.RawMessage {
return t.TransientPayload
}

func (t *Flow) ToLoggerField() map[string]interface{} {
func (t Flow) ToLoggerField() map[string]interface{} {
return map[string]interface{}{
"id": t.ID.String(),
"return_to": t.ReturnTo,
Expand Down
9 changes: 6 additions & 3 deletions selfservice/flow/registration/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ func (s *ErrorHandler) WriteFlowError(
err = schema.NewDuplicateCredentialsError(dup)
}

s.d.Audit().
logger := s.d.Audit().
WithError(err).
WithRequest(r).
WithField("registration_flow", flow.ToLoggerField(f)).
WithRequest(r)
if f != nil {
logger = logger.WithField("registration_flow", f.ToLoggerField())
}
logger.
Info("Encountered self-service flow error.")

if f == nil {
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/registration/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (f *Flow) SetReturnToVerification(to string) {
f.ReturnToVerification = to
}

func (f *Flow) ToLoggerField() map[string]interface{} {
func (f Flow) ToLoggerField() map[string]interface{} {
return map[string]interface{}{
"id": f.ID.String(),
"return_to": f.ReturnTo,
Expand Down
12 changes: 8 additions & 4 deletions selfservice/flow/settings/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,15 @@ func (s *ErrorHandler) WriteFlowError(
id *identity.Identity,
err error,
) {
s.d.Audit().
logger := s.d.Audit().
WithError(err).
WithRequest(r).
WithField("settings_flow", flow.ToLoggerField(f)).
Info("Encountered self-service settings error.")
WithRequest(r)

if f != nil {
logger = logger.
WithField("settings_flow", f.ToLoggerField())
}
logger.Info("Encountered self-service settings error.")

shouldRespondWithJSON := x.IsJSONRequest(r)
if f != nil && f.Type == flow.TypeAPI {
Expand Down
10 changes: 7 additions & 3 deletions selfservice/flow/verification/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ func (s *ErrorHandler) WriteFlowError(
group node.UiNodeGroup,
err error,
) {
s.d.Audit().
logger := s.d.Audit().
WithError(err).
WithRequest(r).
WithField("verification_flow", flow.ToLoggerField(f)).
WithRequest(r)

if f != nil {
logger = logger.WithField("verification_flow", f.ToLoggerField())
}
logger.
Info("Encountered self-service verification error.")

if f == nil {
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/verification/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (t *Flow) GetTransientPayload() json.RawMessage {
return t.TransientPayload
}

func (f *Flow) ToLoggerField() map[string]interface{} {
func (f Flow) ToLoggerField() map[string]interface{} {
return map[string]interface{}{
"id": f.ID.String(),
"return_to": f.ReturnTo,
Expand Down

0 comments on commit afbcfb2

Please sign in to comment.