Coffee journaling on ATProto (alpha) alpha.arabica.social
coffee

fix: improve join/create error message logging

pdewey.com 4d52dda0 4a8ae651

verified
+26 -4
+26 -4
internal/handlers/join.go
··· 123 } 124 125 // Create invite code via PDS admin API 126 client := &xrpc.Client{ 127 Host: h.pdsAdminURL, 128 AdminToken: &h.pdsAdminToken, ··· 131 UseCount: 1, 132 }) 133 if err != nil { 134 - log.Error().Err(err).Str("email", reqEmail).Msg("Failed to create invite code") 135 http.Error(w, "Failed to create invite code", http.StatusInternalServerError) 136 return 137 } ··· 140 141 // Email the invite code to the requester 142 if h.emailSender != nil && h.emailSender.Enabled() { 143 subject := "Your Arabica Invite Code" 144 // TODO: this should probably use the env var rather than hard coded (for name/url) 145 // TODO: also this could be a template file 146 body := fmt.Sprintf("Welcome to Arabica!\n\nHere is your invite code to create an account on the arabica.systems PDS:\n\n %s\n\nVisit https://arabica.social/join to sign up with this code.\n\nHappy brewing!\n", out.Code) 147 if err := h.emailSender.Send(reqEmail, subject, body); err != nil { 148 - log.Error().Err(err).Str("email", reqEmail).Msg("Failed to send invite email") 149 http.Error(w, "Invite created but failed to send email. Code: "+out.Code, http.StatusInternalServerError) 150 return 151 } 152 - log.Info().Str("email", reqEmail).Msg("Invite code emailed") 153 } 154 155 // Log the action ··· 317 } 318 319 // Call PDS createAccount (public endpoint, no admin token needed) 320 client := &xrpc.Client{Host: h.pdsAdminURL} 321 out, err := comatproto.ServerCreateAccount(r.Context(), client, &comatproto.ServerCreateAccount_Input{ 322 Handle: fullHandle, ··· 344 } 345 } 346 } 347 - log.Error().Err(err).Str("handle", fullHandle).Msg("Failed to create account") 348 renderError(errMsg) 349 return 350 }
··· 123 } 124 125 // Create invite code via PDS admin API 126 + log.Info().Str("pds_url", h.pdsAdminURL).Str("email", reqEmail).Msg("Creating invite code via PDS admin API") 127 client := &xrpc.Client{ 128 Host: h.pdsAdminURL, 129 AdminToken: &h.pdsAdminToken, ··· 132 UseCount: 1, 133 }) 134 if err != nil { 135 + logEvent := log.Error().Err(err).Str("email", reqEmail).Str("pds_url", h.pdsAdminURL) 136 + var xrpcErr *xrpc.Error 137 + if errors.As(err, &xrpcErr) { 138 + logEvent = logEvent.Int("status_code", xrpcErr.StatusCode) 139 + var inner *xrpc.XRPCError 140 + if errors.As(xrpcErr.Wrapped, &inner) { 141 + logEvent = logEvent.Str("xrpc_error", inner.ErrStr).Str("xrpc_message", inner.Message) 142 + } 143 + } 144 + logEvent.Msg("Failed to create invite code") 145 http.Error(w, "Failed to create invite code", http.StatusInternalServerError) 146 return 147 } ··· 150 151 // Email the invite code to the requester 152 if h.emailSender != nil && h.emailSender.Enabled() { 153 + log.Info().Str("email", reqEmail).Str("code", out.Code).Msg("Sending invite code email") 154 subject := "Your Arabica Invite Code" 155 // TODO: this should probably use the env var rather than hard coded (for name/url) 156 // TODO: also this could be a template file 157 body := fmt.Sprintf("Welcome to Arabica!\n\nHere is your invite code to create an account on the arabica.systems PDS:\n\n %s\n\nVisit https://arabica.social/join to sign up with this code.\n\nHappy brewing!\n", out.Code) 158 if err := h.emailSender.Send(reqEmail, subject, body); err != nil { 159 + log.Error().Err(err).Str("email", reqEmail).Str("code", out.Code).Msg("Failed to send invite email") 160 http.Error(w, "Invite created but failed to send email. Code: "+out.Code, http.StatusInternalServerError) 161 return 162 } 163 + log.Info().Str("email", reqEmail).Msg("Invite code emailed successfully") 164 + } else { 165 + reason := "nil" 166 + if h.emailSender != nil { 167 + reason = "disabled (no SMTP host)" 168 + } 169 + log.Warn().Str("email", reqEmail).Str("code", out.Code).Str("reason", reason).Msg("Email sender not available, invite code not emailed") 170 } 171 172 // Log the action ··· 334 } 335 336 // Call PDS createAccount (public endpoint, no admin token needed) 337 + log.Info().Str("handle", fullHandle).Str("email", emailAddr).Str("pds_url", h.pdsAdminURL).Msg("Creating account via PDS") 338 client := &xrpc.Client{Host: h.pdsAdminURL} 339 out, err := comatproto.ServerCreateAccount(r.Context(), client, &comatproto.ServerCreateAccount_Input{ 340 Handle: fullHandle, ··· 362 } 363 } 364 } 365 + logEvent := log.Error().Err(err).Str("handle", fullHandle).Str("email", emailAddr).Str("pds_url", h.pdsAdminURL) 366 + if xrpcErr2, ok := err.(*xrpc.Error); ok { 367 + logEvent = logEvent.Int("status_code", xrpcErr2.StatusCode) 368 + } 369 + logEvent.Msg("Failed to create account") 370 renderError(errMsg) 371 return 372 }