Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Request Handling and Enhance Error Logging in general.py #28

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

Mefisto04
Copy link
Contributor

This PR refactors the request handling in the codebase by:

  1. Centralizing Response Handling: Extracted HTTP response handling into a new handle_response function to reduce code duplication in request and arequest.

  2. Improving Error Logging: Enhanced error logging with more details for various HTTP statuses.

Code Snippet:

New handle_response Function:

def handle_response(res, json, url):
    if res.status_code < 299:
        return res.json()
    elif res.status_code == 404:
        logging.error("Request URL: {} | Error[404]: 请求错误: 错误的地址".format(url))
        raise VQLError(516)
    elif res.status_code == 422:
        logging.error(
            "Request URL: {} | Request body: {} | Error[422]: 请求错误: 错误的请求格式".format(
                url, json
            )
        )
        raise VQLError(517)
    else:
        info = res.json()
        logging.error(
            "Request URL: {} | Request body: {} | Error: {}".format(url, json, info)
        )
        raise VQLError(511, detail=info)

Updated Functions:

  • request:

    def request(json: dict, url: str, headers: dict | None = None) -> dict:
        ...
        return handle_response(res, json, url)
  • arequest:

    async def arequest(url: str, json: dict, headers: dict | None = None) -> dict:
        ...
        return handle_response(res, json, url)

Conclusion:

These changes enhance maintainability and clarity in error handling. Please review.

@Mefisto04
Copy link
Contributor Author

hey @panregedit please review this changes that i have made with the issue #27

@panregedit panregedit merged commit 15fcf81 into om-ai-lab:main Oct 22, 2024
@panregedit
Copy link
Collaborator

Thank you for the optimization! The code has been merged into the project.

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.

2 participants