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 fastapi
|
||||
import pydantic
|
||||
import starlette
|
||||
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
from pydantic.error_wrappers import ErrorWrapper
|
||||
|
||||
from yunohost.interface.base import (
|
||||
BaseInterface,
|
||||
InterfaceKind,
|
||||
|
@ -15,6 +19,7 @@ from yunohost.interface.base import (
|
|||
get_params_doc,
|
||||
override_function,
|
||||
)
|
||||
from yunohost.utils.error import YunohostValidationError
|
||||
|
||||
|
||||
def snake_to_camel_case(snake: str) -> str:
|
||||
|
@ -134,24 +139,35 @@ class Interface(BaseInterface):
|
|||
|
||||
override_params.append(param.replace(default=param_default))
|
||||
|
||||
if as_body:
|
||||
|
||||
def body_to_args_back(*args, **kwargs):
|
||||
def hook_results(*args, **kwargs):
|
||||
new_kwargs = {}
|
||||
for kwarg, value in kwargs.items():
|
||||
if issubclass(type(value), pydantic.BaseModel):
|
||||
opened_files = []
|
||||
|
||||
for name, value in kwargs.items():
|
||||
if isinstance(value, pydantic.BaseModel):
|
||||
# Turn pydantic model back to individual kwargs
|
||||
new_kwargs = value.dict() | new_kwargs
|
||||
else:
|
||||
new_kwargs[kwarg] = value
|
||||
new_kwargs[name] = value
|
||||
|
||||
try:
|
||||
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(
|
||||
func, signature, override_params, decorator=body_to_args_back
|
||||
func,
|
||||
signature,
|
||||
override_params,
|
||||
decorator=hook_results,
|
||||
doc=func.__doc__.split("\f")[0] if func.__doc__ else None,
|
||||
)
|
||||
else:
|
||||
route_func = override_function(func, signature, override_params)
|
||||
|
||||
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)(
|
||||
route, summary=summary, deprecated=local_data.get("deprecated")
|
||||
)(route_func)
|
||||
|
|
|
@ -80,9 +80,14 @@ class Interface(BaseInterface):
|
|||
override_params.append(param.replace(default=param_default))
|
||||
|
||||
def hook_results(*args, **kwargs):
|
||||
try:
|
||||
results = func(*args, **kwargs)
|
||||
print_as_yaml(results)
|
||||
return results
|
||||
except YunohostValidationError as e:
|
||||
raise typer.BadParameter(e.strerror)
|
||||
except:
|
||||
raise
|
||||
|
||||
command_func = override_function(
|
||||
func,
|
||||
|
|
Loading…
Add table
Reference in a new issue