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 123 } 124 124 125 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") 126 127 client := &xrpc.Client{ 127 128 Host: h.pdsAdminURL, 128 129 AdminToken: &h.pdsAdminToken, ··· 131 132 UseCount: 1, 132 133 }) 133 134 if err != nil { 134 - log.Error().Err(err).Str("email", reqEmail).Msg("Failed to create invite code") 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") 135 145 http.Error(w, "Failed to create invite code", http.StatusInternalServerError) 136 146 return 137 147 } ··· 140 150 141 151 // Email the invite code to the requester 142 152 if h.emailSender != nil && h.emailSender.Enabled() { 153 + log.Info().Str("email", reqEmail).Str("code", out.Code).Msg("Sending invite code email") 143 154 subject := "Your Arabica Invite Code" 144 155 // TODO: this should probably use the env var rather than hard coded (for name/url) 145 156 // TODO: also this could be a template file 146 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) 147 158 if err := h.emailSender.Send(reqEmail, subject, body); err != nil { 148 - log.Error().Err(err).Str("email", reqEmail).Msg("Failed to send invite email") 159 + log.Error().Err(err).Str("email", reqEmail).Str("code", out.Code).Msg("Failed to send invite email") 149 160 http.Error(w, "Invite created but failed to send email. Code: "+out.Code, http.StatusInternalServerError) 150 161 return 151 162 } 152 - log.Info().Str("email", reqEmail).Msg("Invite code emailed") 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") 153 170 } 154 171 155 172 // Log the action ··· 317 334 } 318 335 319 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") 320 338 client := &xrpc.Client{Host: h.pdsAdminURL} 321 339 out, err := comatproto.ServerCreateAccount(r.Context(), client, &comatproto.ServerCreateAccount_Input{ 322 340 Handle: fullHandle, ··· 344 362 } 345 363 } 346 364 } 347 - log.Error().Err(err).Str("handle", fullHandle).Msg("Failed to create account") 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") 348 370 renderError(errMsg) 349 371 return 350 372 }