Skip to content

Fix/ksef sync error logging#62

Merged
emilwojtaszek merged 5 commits into
mainfrom
fix/ksef-sync-error-logging
Feb 28, 2026
Merged

Fix/ksef sync error logging#62
emilwojtaszek merged 5 commits into
mainfrom
fix/ksef-sync-error-logging

Conversation

@emilwojtaszek

@emilwojtaszek emilwojtaszek commented Feb 28, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Chores
    • Enhanced system logging and monitoring across authentication and API interactions for improved observability and troubleshooting.
    • Added clearer handling and logging for rate-limit and API error responses to aid reliability and diagnostics.
    • Included contextual identifiers in error reports to improve issue tracking and faster resolution.

emilwojtaszek and others added 4 commits February 28, 2026 07:31
Log HTTP method, full URL, status code, and truncated response body on
every error path across all 8 KSeF API functions. Also strip trailing
slash from KSEF_API_URL to prevent double-slash URLs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Each auth step (get_challenge, sign_challenge, authenticate_xades,
poll_auth_status, redeem_tokens) now logs its name and duration in ms,
making it easy to pinpoint which step fails in production.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Helps verify the correct environment is targeted when diagnosing
production sync failures.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds NIP to reauth_failed and generic error logs for easier
cross-referencing with KSeF API logs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Feb 28, 2026

Copy link
Copy Markdown

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

📥 Commits

Reviewing files that changed from the base of the PR and between 023a666 and 0e8f925.

📒 Files selected for processing (3)
  • lib/ksef_hub/application.ex
  • lib/ksef_hub/ksef_client/auth.ex
  • lib/ksef_hub/ksef_client/live.ex
 ________________________________________________________________
< KōdoUsagi Gundam is my Japanese cousin. He's a bit more mecha. >
 ----------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).

✏️ Tip: You can disable in-progress messages and the fortune message in your review settings.

Tip

You can customize the tone of the review comments and chat replies.

Configure the tone_instructions setting to customize the tone of the review comments and chat replies. For example, you can set the tone to Act like a strict teacher, Act like a pirate and more.

📝 Walkthrough

Walkthrough

This PR adds comprehensive logging and instrumentation across multiple KSeF client modules to improve observability. Changes include startup-time API URL logging, step-based authentication instrumentation with timing, extensive HTTP response logging with error handling, and enhanced sync worker error messages with company NIP context.

Changes

Cohort / File(s) Summary
Application Startup
lib/ksef_hub/application.ex
Adds Logger initialization and logs the configured KSeF API URL at application startup with a default fallback value.
Authentication Instrumentation
lib/ksef_hub/ksef_client/auth.ex
Introduces step-based logging and timing instrumentation for each authentication phase (challenge retrieval, signing, authentication, polling, token redemption) via a new private step/2 helper function.
HTTP Interaction Logging
lib/ksef_hub/ksef_client/live.ex
Adds extensive Logger statements across HTTP request/response paths including rate-limiting (429) detection, HTML response handling, error logging with contextual URLs and status codes, and updated terminate_session error handling.
Sync Worker Error Logging
lib/ksef_hub/sync/sync_worker.ex
Introduces new lookup_company_nip/1 private function to annotate re-authentication and generic failure logs with company NIP context for improved error traceability.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • appunite/appunite-ksef-ex#28: Adds an AuthWorker that invokes KsefHub.KsefClient.Auth.authenticate, making it directly affected by the new step-based logging instrumentation in auth.ex.

Poem

🐰 With Logger's light, we trace each step with care,
From auth challenges dancing through the air,
HTTP whispers, errors caught and logged,
No bug shall hide when NIP's been tagged!
The code now speaks its secrets clear—
Observability draws near!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The PR title 'Fix/ksef sync error logging' partially describes the changeset but understates its scope—the changes encompass comprehensive logging instrumentation across multiple modules (application startup, auth flow, HTTP interactions, and sync worker), not just sync error logging improvements. Consider a more accurate title like 'Add comprehensive logging instrumentation across KSeF client and sync modules' to better reflect the full scope of changes.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/ksef-sync-error-logging

Comment @coderabbitai help to get the list of available commands and usage tips.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
lib/ksef_hub/ksef_client/auth.ex (1)

1-1: ⚠️ Potential issue | 🟡 Minor

Run mix format to fix the pipeline failure.

The CI pipeline is failing because this file is not properly formatted. All four changed files need formatting.

Fix all formatting issues at once
mix format lib/ksef_hub/application.ex lib/ksef_hub/sync/sync_worker.ex lib/ksef_hub/ksef_client/live.ex lib/ksef_hub/ksef_client/auth.ex
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/ksef_hub/ksef_client/auth.ex` at line 1, The CI failed due to formatting;
run mix format on the affected files and ensure the KsefHub.KsefClient.Auth
module (and the other changed modules) are formatted: execute the provided
command to format lib/ksef_hub/application.ex, lib/ksef_hub/sync/sync_worker.ex,
lib/ksef_hub/ksef_client/live.ex and lib/ksef_hub/ksef_client/auth.ex so the
module definitions (e.g., defmodule KsefHub.KsefClient.Auth) and pipelines
comply with mix format.
lib/ksef_hub/ksef_client/live.ex (1)

1-9: ⚠️ Potential issue | 🟡 Minor

Run mix format to fix the pipeline failure.

The CI pipeline is failing because this file is not properly formatted.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/ksef_hub/ksef_client/live.ex` around lines 1 - 9, The file
KsefHub.KsefClient.Live (module KsefHub.KsefClient.Live) is not formatted
correctly; run the Elixir formatter on this file by executing mix format (or
format the project) so the module definition, `@moduledoc`, `@behaviour` and require
Logger lines conform to the project's formatting rules and fix the CI pipeline
failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/ksef_hub/sync/sync_worker.ex`:
- Around line 292-298: Run the Elixir formatter to fix CI failure: apply mix
format to the module containing lookup_company_nip/1 (the `@spec` and defp
lookup_company_nip function) so the spacing and pipeline formatting conform to
project style; you can run `mix format` locally or format that function block to
match the repo conventions.

---

Outside diff comments:
In `@lib/ksef_hub/ksef_client/auth.ex`:
- Line 1: The CI failed due to formatting; run mix format on the affected files
and ensure the KsefHub.KsefClient.Auth module (and the other changed modules)
are formatted: execute the provided command to format
lib/ksef_hub/application.ex, lib/ksef_hub/sync/sync_worker.ex,
lib/ksef_hub/ksef_client/live.ex and lib/ksef_hub/ksef_client/auth.ex so the
module definitions (e.g., defmodule KsefHub.KsefClient.Auth) and pipelines
comply with mix format.

In `@lib/ksef_hub/ksef_client/live.ex`:
- Around line 1-9: The file KsefHub.KsefClient.Live (module
KsefHub.KsefClient.Live) is not formatted correctly; run the Elixir formatter on
this file by executing mix format (or format the project) so the module
definition, `@moduledoc`, `@behaviour` and require Logger lines conform to the
project's formatting rules and fix the CI pipeline failure.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0eeabe2 and 023a666.

📒 Files selected for processing (4)
  • lib/ksef_hub/application.ex
  • lib/ksef_hub/ksef_client/auth.ex
  • lib/ksef_hub/ksef_client/live.ex
  • lib/ksef_hub/sync/sync_worker.ex

Comment on lines +292 to +298
@spec lookup_company_nip(Ecto.UUID.t()) :: String.t()
defp lookup_company_nip(company_id) do
case KsefHub.Companies.get_company(company_id) do
%{nip: nip} -> nip
_ -> "unknown"
end
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Run mix format to fix the pipeline failure.

The CI pipeline is failing due to formatting. The lookup_company_nip/1 implementation is clean—using pattern matching and a sensible fallback. NIP (tax ID) is a business identifier that aids debugging without exposing sensitive credentials.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/ksef_hub/sync/sync_worker.ex` around lines 292 - 298, Run the Elixir
formatter to fix CI failure: apply mix format to the module containing
lookup_company_nip/1 (the `@spec` and defp lookup_company_nip function) so the
spacing and pipeline formatting conform to project style; you can run `mix
format` locally or format that function block to match the repo conventions.

@emilwojtaszek emilwojtaszek merged commit d1c7382 into main Feb 28, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant