mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
test temp error handling
This commit is contained in:
parent
de7f2adb4c
commit
d677cf5eac
2 changed files with 38 additions and 17 deletions
|
@ -6,8 +6,12 @@ import inspect
|
||||||
import re
|
import re
|
||||||
import fastapi
|
import fastapi
|
||||||
import pydantic
|
import pydantic
|
||||||
|
import starlette
|
||||||
|
|
||||||
from typing import Any, Optional, Union
|
from typing import Any, Optional, Union
|
||||||
|
|
||||||
|
from pydantic.error_wrappers import ErrorWrapper
|
||||||
|
|
||||||
from yunohost.interface.base import (
|
from yunohost.interface.base import (
|
||||||
BaseInterface,
|
BaseInterface,
|
||||||
InterfaceKind,
|
InterfaceKind,
|
||||||
|
@ -15,6 +19,7 @@ from yunohost.interface.base import (
|
||||||
get_params_doc,
|
get_params_doc,
|
||||||
override_function,
|
override_function,
|
||||||
)
|
)
|
||||||
|
from yunohost.utils.error import YunohostValidationError
|
||||||
|
|
||||||
|
|
||||||
def snake_to_camel_case(snake: str) -> str:
|
def snake_to_camel_case(snake: str) -> str:
|
||||||
|
@ -134,24 +139,35 @@ class Interface(BaseInterface):
|
||||||
|
|
||||||
override_params.append(param.replace(default=param_default))
|
override_params.append(param.replace(default=param_default))
|
||||||
|
|
||||||
if as_body:
|
def hook_results(*args, **kwargs):
|
||||||
|
new_kwargs = {}
|
||||||
|
opened_files = []
|
||||||
|
|
||||||
def body_to_args_back(*args, **kwargs):
|
for name, value in kwargs.items():
|
||||||
new_kwargs = {}
|
if isinstance(value, pydantic.BaseModel):
|
||||||
for kwarg, value in kwargs.items():
|
# Turn pydantic model back to individual kwargs
|
||||||
if issubclass(type(value), pydantic.BaseModel):
|
new_kwargs = value.dict() | new_kwargs
|
||||||
new_kwargs = value.dict() | new_kwargs
|
else:
|
||||||
else:
|
new_kwargs[name] = value
|
||||||
new_kwargs[kwarg] = value
|
|
||||||
|
try:
|
||||||
return func(*args, **new_kwargs)
|
return func(*args, **new_kwargs)
|
||||||
|
except YunohostValidationError as e:
|
||||||
|
# Try to mimic Pydantic validation errors
|
||||||
|
# FIXME replace dummy error information
|
||||||
|
raise fastapi.exceptions.RequestValidationError([ErrorWrapper(ValueError(e.strerror), ("query", "test"))])
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
|
||||||
route_func = override_function(
|
route_func = override_function(
|
||||||
func, signature, override_params, decorator=body_to_args_back
|
func,
|
||||||
)
|
signature,
|
||||||
else:
|
override_params,
|
||||||
route_func = override_function(func, signature, override_params)
|
decorator=hook_results,
|
||||||
|
doc=func.__doc__.split("\f")[0] if func.__doc__ else None,
|
||||||
|
)
|
||||||
|
|
||||||
summary = func.__doc__.split("\n\n")[0] if func.__doc__ else None
|
summary = func.__doc__.split("\b")[0] if func.__doc__ else None
|
||||||
getattr(self.instance, method)(
|
getattr(self.instance, method)(
|
||||||
route, summary=summary, deprecated=local_data.get("deprecated")
|
route, summary=summary, deprecated=local_data.get("deprecated")
|
||||||
)(route_func)
|
)(route_func)
|
||||||
|
|
|
@ -80,9 +80,14 @@ class Interface(BaseInterface):
|
||||||
override_params.append(param.replace(default=param_default))
|
override_params.append(param.replace(default=param_default))
|
||||||
|
|
||||||
def hook_results(*args, **kwargs):
|
def hook_results(*args, **kwargs):
|
||||||
results = func(*args, **kwargs)
|
try:
|
||||||
print_as_yaml(results)
|
results = func(*args, **kwargs)
|
||||||
return results
|
print_as_yaml(results)
|
||||||
|
return results
|
||||||
|
except YunohostValidationError as e:
|
||||||
|
raise typer.BadParameter(e.strerror)
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
|
||||||
command_func = override_function(
|
command_func = override_function(
|
||||||
func,
|
func,
|
||||||
|
|
Loading…
Add table
Reference in a new issue