-
Notifications
You must be signed in to change notification settings - Fork 32
🎨 web-api: Simpler error models and auto-generated errors in OAS #6855
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
131fb7b
new model
pcrespov 835b190
using new model
pcrespov 4bc45e8
logging
pcrespov 1a5dee9
minor
pcrespov a8aaea0
oas
pcrespov e5abec1
oas
pcrespov 15adfaf
error enveloped
pcrespov 4a7a3ed
fix
pcrespov e604522
updates error envelope
pcrespov b1d545a
fixes agent
pcrespov ee8fd6c
mypy
pcrespov a29b11b
Merge branch 'master' into mai/error-models
pcrespov 1f1770a
Merge branch 'master' into mai/error-models
pcrespov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/models-library/src/models_library/rest_error.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Annotated | ||
|
|
||
| from models_library.generics import Envelope | ||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
| from .basic_types import IDStr, LogLevel | ||
|
|
||
|
|
||
| class Log(BaseModel): | ||
| level: LogLevel | None = Field("INFO", description="log level") | ||
| message: str = Field( | ||
| ..., | ||
| description="log message. If logger is USER, then it MUST be human readable", | ||
| ) | ||
| logger: str | None = Field( | ||
| None, description="name of the logger receiving this message" | ||
| ) | ||
|
|
||
| model_config = ConfigDict( | ||
| json_schema_extra={ | ||
| "example": { | ||
| "message": "Hi there, Mr user", | ||
| "level": "INFO", | ||
| "logger": "user-logger", | ||
| } | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class ErrorItem(BaseModel): | ||
| code: str = Field( | ||
| ..., | ||
| description="Typically the name of the exception that produced it otherwise some known error code", | ||
| ) | ||
| message: str = Field(..., description="Error message specific to this item") | ||
| resource: str | None = Field( | ||
| None, description="API resource affected by this error" | ||
| ) | ||
| field: str | None = Field(None, description="Specific field within the resource") | ||
|
|
||
|
|
||
| @dataclass | ||
| class LogMessageType: | ||
| # NOTE: deprecated! | ||
| message: str | ||
| level: str = "INFO" | ||
| logger: str = "user" | ||
|
|
||
|
|
||
| @dataclass | ||
| class ErrorItemType: | ||
| # NOTE: deprecated! | ||
| code: str | ||
| message: str | ||
| resource: str | None | ||
| field: str | None | ||
|
|
||
| @classmethod | ||
| def from_error(cls, err: BaseException): | ||
| return cls( | ||
| code=err.__class__.__name__, message=str(err), resource=None, field=None | ||
| ) | ||
|
|
||
|
|
||
| class ErrorGet(BaseModel): | ||
| message: Annotated[ | ||
| str, | ||
| Field( | ||
| min_length=5, | ||
| description="Message displayed to the user", | ||
| ), | ||
| ] | ||
| support_id: Annotated[ | ||
| IDStr | None, | ||
| Field(description="ID to track the incident during support", alias="supportId"), | ||
| ] = None | ||
|
|
||
| # NOTE: The fields blow are DEPRECATED. Still here to keep compatibilty with front-end until updated | ||
| status: Annotated[int, Field(deprecated=True)] = 400 | ||
| errors: Annotated[ | ||
| list[ErrorItemType], | ||
| Field(deprecated=True, default_factory=list, json_schema_extra={"default": []}), | ||
| ] | ||
| logs: Annotated[ | ||
| list[LogMessageType], | ||
| Field(deprecated=True, default_factory=list, json_schema_extra={"default": []}), | ||
| ] | ||
|
|
||
| model_config = ConfigDict( | ||
| populate_by_name=True, | ||
| extra="ignore", # Used to prune extra fields from internal data | ||
| frozen=True, | ||
| json_schema_extra={ | ||
| "examples": [ | ||
| { | ||
| "message": "Sorry you do not have sufficient access rights for product" | ||
| }, | ||
| { | ||
| "message": "Opps this error was unexpected. We are working on that!", | ||
| "supportId": "OEC:12346789", | ||
| }, | ||
| ] | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| class EnvelopedError(Envelope[None]): | ||
| error: ErrorGet | ||
|
|
||
| model_config = ConfigDict( | ||
| json_schema_extra={ | ||
| "examples": [ | ||
| {"error": {"message": "display error message here"}}, | ||
| { | ||
| "error": {"message": "failure", "supportId": "OEC:123455"}, | ||
| "data": None, | ||
| }, | ||
| ] | ||
| }, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.