Skip to content

Conversation

@medcl
Copy link
Member

@medcl medcl commented Jan 2, 2026

What does this PR do

Rationale for this change

Standards checklist

  • The PR title is descriptive
  • The commit messages are semantic
  • Necessary tests are added
  • Updated the release notes
  • Necessary documents have been added if this is a new feature
  • Performance tests checked, no obvious performance degradation

Copilot AI review requested due to automatic review settings January 2, 2026 11:48
@medcl medcl marked this pull request as draft January 2, 2026 11:48
@medcl medcl marked this pull request as ready for review January 2, 2026 11:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors HTTP error handling throughout the codebase to provide better control over HTTP status codes in error responses. The refactoring introduces new error handling functions that can attach HTTP status codes directly to errors, and updates the panic recovery middleware to extract and use these codes when responding to panicked requests.

Key Changes:

  • Introduced ErrorWithHTTPCode and NewWithHTTPCode functions to create errors with HTTP status codes
  • Updated panic recovery handler to extract HTTP codes from errors
  • Added context parameter to EntityProvider interface methods for better request tracing
  • Refactored OrganizationPrincipal schema and introduced OrganizationPrincipalCache
  • Added comprehensive test coverage for new Elasticsearch document handling functions

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
core/errors/error.go Added HTTP code support to error types and functions; refactored Cause/Code functions
core/api/recovery.go Updated panic handler to extract and use HTTP codes from errors
core/api/util.go Added helper functions for common HTTP error responses (401, 403, 400)
core/util/http.go Updated to use new ErrorWithCode function name
core/security/*.go Updated error handling to use HTTP code errors; added context to functions
core/entity_card/api.go Added context parameter to entity provider interface methods
core/orm/*.go Added debug-mode panics for validation errors; added system fields support
core/elastic/*.go Added generic document helpers and tests; refactored response structures
core/global/register.go Added InitialDelay field to BackgroundTask for better task scheduling
modules/security/rbac/*.go Updated to use context in entity providers; refactored principal caching
modules/elastic/schema.go Consolidated log messages for better readability
modules/queue/disk_queue/module.go Fixed formatting (spacing after else)
docs/content.en/docs/release-notes/_index.md Added release note for principal cache refactoring

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 260 to 266
if err != nil {
cause, ok := err.(causer)
if !ok {
break
if ok {
err = cause.Cause()
}
err = cause.Cause()
}
return err
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

The refactored Cause() function only unwraps one level of error causes, whereas the original implementation would recursively unwrap all nested causes until reaching the root cause. This changes the behavior of the function and may break code that depends on getting the root cause. Consider restoring the loop to maintain backward compatibility:

for err != nil {
    cause, ok := err.(causer)
    if !ok {
        break
    }
    err = cause.Cause()
}

Copilot uses AI. Check for mistakes.
v := u.Provider != "" && u.Login != "" && u.UserID != ""
if !v {
if global.Env().IsDebug {
log.Error(util.MustToJSON(u), u.UserID)
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

Incorrect indentation for the log.Error statement. It should be indented to be inside the if block.

Suggested change
log.Error(util.MustToJSON(u), u.UserID)
log.Error(util.MustToJSON(u), u.UserID)

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +78
if global.Env().IsDebug {
panic(errors.NewWithHTTPCode(401, "user not found"))
}

Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

Inconsistent behavior between debug and non-debug modes. In debug mode, the function panics when user is not found, but in non-debug mode it returns an error. This makes the function behavior unpredictable and can cause issues when moving code between environments. Consider either always panicking (since the caller can use MustGetUserFromContext for that) or always returning an error consistently.

Suggested change
if global.Env().IsDebug {
panic(errors.NewWithHTTPCode(401, "user not found"))
}

Copilot uses AI. Check for mistakes.
Comment on lines 276 to 282
if err != nil {
cause, ok := err.(causer)
if ok {
code = cause.Code()
}
}
return code
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

The refactored Code() function only checks one level of error wrapping, whereas the original implementation would loop through all nested causes to find an error code. This changes the behavior and may not find the error code when errors are wrapped multiple times. Consider restoring the loop to maintain backward compatibility:

for err != nil {
    cause, ok := err.(causer)
    if !ok {
        break
    }
    code = cause.Code()
    err = cause.Cause()
}

Copilot uses AI. Check for mistakes.
Comment on lines 291 to 297
if err != nil {
cause, ok := err.(causer)
if !ok {
break
if ok {
code = cause.HTTPCode()
}
code = cause.Code()
}
return code
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

The HTTPCode() function only checks one level of error wrapping. For consistency with how the original Code() and Cause() functions worked, this should loop through nested causes to find the HTTP code. Consider implementing it similarly:

for err != nil {
    cause, ok := err.(causer)
    if !ok {
        break
    }
    code = cause.HTTPCode()
    err = cause.Cause()
}

This ensures HTTP codes are properly extracted from deeply nested error chains.

Copilot uses AI. Check for mistakes.
@SteveLauC SteveLauC merged commit 1aaf89f into main Jan 4, 2026
4 checks passed
@SteveLauC SteveLauC deleted the refactoring_http_error branch January 4, 2026 06:34
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.

4 participants