-
Notifications
You must be signed in to change notification settings - Fork 6
Generic error catching in DB #152
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8de5078
error class
max-zilla d3a81d7
clean up arg defaults
max-zilla 0a3f720
formatting
max-zilla fbd7b56
Merge branch 'main' into generic-error-class
lmarini a534018
fix documentation
max-zilla 90da530
Merge branch 'generic-error-class' of https://github.com/clowder-fram…
max-zilla 3d9dfba
add await to log_error
max-zilla 097db43
update ES connects to async
max-zilla d42d7c1
Merge branch 'main' into generic-error-class
max-zilla 2fbc64e
Fix circular import
max-zilla 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
| import logging | ||
| import traceback | ||
| import motor.motor_asyncio | ||
| from typing import Optional, Generator | ||
| from fastapi import Depends | ||
| from pymongo import MongoClient | ||
|
|
||
| from app.config import settings | ||
| from app.mongo import crete_mongo_indexes | ||
| from app.models.errors import Error | ||
| from app.models.metadata import MongoDBRef | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def _get_db() -> Generator: | ||
| """Duplicate of app.dependencies.get_db(), but importing that causes circular import.""" | ||
| mongo_client = motor.motor_asyncio.AsyncIOMotorClient(settings.MONGODB_URL) | ||
| db = mongo_client[settings.MONGO_DATABASE] | ||
| await crete_mongo_indexes(db) | ||
| yield db | ||
|
|
||
|
|
||
| async def log_error( | ||
| exception: Exception, | ||
| resource: Optional[MongoDBRef] = None, | ||
| user: Optional[str] = None, | ||
| ): | ||
| """Insert new Error into the database. | ||
|
|
||
| Arguments: | ||
| exception -- instance of an Exception or subclass | ||
| resource -- if error relates to a specific resource, you can include it | ||
| user --- if error relates to actions performed by a user, you can include them | ||
| """ | ||
| db = _get_db() | ||
| message = str(exception) | ||
| trace = traceback.format_exc(exception, limit=4) | ||
|
|
||
| logger.error(message) | ||
| error_log = Error(message=message, trace=trace, resource=resource, user=user) | ||
| await db["errors"].insert_one(error_log.to_mongo()) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from datetime import datetime | ||
| from typing import Optional | ||
| from pydantic import Field | ||
|
|
||
| from app.models.mongomodel import MongoModel | ||
| from app.models.metadata import MongoDBRef | ||
|
|
||
|
|
||
| class ServiceUnreachable(Exception): | ||
| """Raised when Clowder can't connect to an outside service e.g. MongoDB, Elasticsearch.""" | ||
|
|
||
| def __init__(self, service, *args): | ||
| super().__init__(args) | ||
| self.service = service | ||
|
|
||
| def __str__(self): | ||
| return f"{self.service} could not be reached." | ||
|
|
||
|
|
||
| class Error(MongoModel): | ||
| message: str # Shorthand message of the error | ||
| trace: str # Full stack trace of the error | ||
| resource: Optional[MongoDBRef] = None | ||
| user_id: Optional[str] = None | ||
| timestamp: datetime = Field(default_factory=datetime.utcnow) |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good, but I am not sure how to test it in this case. If I turn off elasticsearch, I get an error before we get to ping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this was 1) start up docker ES, 2) start up backend, 3) kill docker ES?
otherwise, we could come up with a contrived test case like "if es.ping() =.TRUE, pretend it failed" just to check the actual logging of errors even if the error is nonsense.