1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-11-09 07:33:16 +01:00

Improve message in 301: Moved Permanently error in git-obs

This commit is contained in:
2025-05-09 13:50:56 +02:00
parent 79657c0e22
commit d5ed8fe2e4

View File

@@ -1,4 +1,5 @@
import inspect
import json
import re
from typing import Optional
@@ -17,8 +18,11 @@ def response_to_exception(response: GiteaHTTPResponse, *, context: Optional[dict
- in some cases, it's required to provide additional context to the request, that is passed to the raised exception,
for example: ``conn.request("GET", url, context={"owner": owner, "repo": repo})``
"""
data = response.json()
messages = [data["message"]] + (data.get("errors", None) or [])
try:
data = response.json()
messages = [data["message"]] + (data.get("errors", None) or [])
except json.JSONDecodeError:
messages = [response.data.decode("utf-8")]
for cls in EXCEPTION_CLASSES:
if cls.RESPONSE_STATUS is not None and cls.RESPONSE_STATUS != response.status:
@@ -57,6 +61,25 @@ class GiteaException(oscerr.OscBaseError):
return result
class MovedPermanently(GiteaException):
RESPONSE_STATUS = 301
RESPONSE_MESSAGE_RE = [
re.compile(r"(?P<message>.*)"),
]
def __init__(self, response: GiteaHTTPResponse, message: str):
super().__init__(response)
self.message = message
def __str__(self):
result = (
f"{self.RESPONSE_STATUS} Moved Permanently: {self.message}\n"
" * Change Gitea URL to the actual location of the service\n"
" * Check Gitea URL for errors and typos such as https:// vs http://"
)
return result
class BranchDoesNotExist(GiteaException):
RESPONSE_STATUS = 404
# modules/git/error.go: return fmt.Sprintf("branch does not exist [name: %s]", err.Name)