We use mongouse version 0.4.2 and have a piece of code that looks like this:
from mongoengine import Document
class Job(Document):
.....
def create_and_save_job(cls, my_data):
try:
j = cls(**my_data)
j.save()
except Exception as e:
if "duplicate unique keys" in e.message:
return
else:
raise
logger.info({"id": j.id, "status": "success"})
We use sentry to capture any exception raised in our code.
We later found out that we logged a lot of id that don't actually exist in the Job collection, and sentry doesn't have any exception related to this.
(something like)
log: {"id": 1234, "status": "success"}
>> db.job.findOne({_id: ObjectId(1234)})
null
What could be causing this? Should we place the save with insert_one and reload after save?
Thanks,
Related
according to werkzeug docs, InternalServerError has original_exception parameter. That looks cool and I'would like to use it in following way to handle exceptions:
#app.errorhandler(Exception)
def handle_errors(err):
return err if isinstance(err, HTTPException) else InternalServerError(original_exception=err)
and log it in #app.after_request method to pack all additional info from request and response, where standard request logging occurs. Unfortunatelly I'm not able to find out how to use stored original_exception. Where is it stored?
When I check sys.exc_info() in #app.after_request, it's also empty (or (None, None, None)).
One way is probably log exceptions directly in #app.errorhandler, request info should be accessible there and response is not ready (because of error) anyways, but what happens with original_exception after storing in InternalServerError???
Thanks for answer, Michal
I found myself here looking for the same thing - how to access the original_exception, but I eventually gave up and settled on traceback to give me the info I was seeking.
This is the key component, capturing the traceback stack as a list:
formatted_lines = traceback.format_exc().splitlines()
Here's my final code in which I extracted the line number of the error and the specific trigger, then passed them to the page:
from werkzeug.exceptions import InternalServerError
import traceback
#app.errorhandler(InternalServerError)
def handle_500(e):
templateData = {
'errorAt' : 'Internal Server Error',
'errorIs' : 'Check ~/www/gunicorn.error for details'
}
try:
formatted_lines = traceback.format_exc().splitlines()
for error_location in formatted_lines:
if 'intvlm8r.py' in error_location:
templateData['errorAt'] = error_location
templateData['errorIs'] = formatted_lines[-1]
except Exception as e:
app.logger.debug(f'handle_500: Unhandled exception: {e}')
return render_template('500_generic.html', **templateData)
I have a function like this to create a github repo and return it.
def CreateGitHubRepo(token, repo_name):
# instantiate github account
g = Github(token)
# create authenticated user
authed_user = g.get_user()
# create a new repo
repo = authed_user.create_repo(repo_name)
return repo
However if a repo already exists with the same name I get an error raised github.GithubException.GithubException: 422 {"message": "Repository creation failed.", "errors": [{"resource": "Repository", "code": "custom", "field": "name", "message": "name already exists on this account"}], "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"}
My question is how could I handle this in my script to catch this error and move forward e.g.
try:
NewRepo = CreateGitHubRepo(token, repo_name)
print("Created New Git Repo: %s" % repo_name)
print(NewRepo)
except GithubException as err:
print('test')
I've tried all the ways I can think of to get the except to catch that error and I'm a little confused.
Answer, I wasn't importing the github module as desired.
I had
from github import Github
so wasn't including the GithubException class.
Lesson, check the imports are doing what you expect.
try:
cursor = connection.cursor()
cursor.execute('update m_password set password = :new_password , updater_id = :updater_id_dic , update_date = :update_date_dic where user_cd = :user_cd_dic and del_flg = :del_flag_dic', queryDictionary)
# Commit
connection.commit()
# Close the cursor
cursor.close()
# Close the database connection
connection.close()
return {
'status': "success",
'errorCode': 0,
'errorMessage': json.dumps("")
}
except: cx_Oracle.IntegrityError as e:
errorObj, = e.args
return {
'status': "error",
'errorCode': errorObj.code,
'errorMessage': json.dumps(errorObj.message)
I am trying to update the value of oracle database . Database connection is alright . I can get the value from database . But update the value is not working . It shows success but value is not actually updated . Also there is no error .
also lambda log showing successfully executed . Please check this image
[1]: https://i.stack.imgur.com/PtLdy.png
I am stuck here .It will be really helpful if i get some help .
Thank you.
It's likely that you're getting an error other than an IntegrityError and don't have an explicit error handler for it. This would cause you to continue towards your this is fine return method.
In fact, updating a password shouldn't throw an integrity error at all since you're not operating on something that effects your schema or is effected by a schema constraint. According to the docs:
This exception is raised when the relational integrity of the data is affected. For example, a duplicate key was inserted or a foreign key constraint would fail.
To troubleshoot, I'd start by accepting any error here. Once you've determined what error type you've caught, you can add per exception error handling as needed.
except Exception as e:
errorObj, = e.args
return {
...
One of my view in Django executes save operations on 6-7 tables. I want these transactions to be atomic I,e if the 5th or 6th transaction fail I want to rollback all the previous saves.
The view contains a try-except block to handle the exceptions raised.
It looks something like this:
#transaction.atomic
def my_view(request):
sid = transaction.savepoint()
try:
Table1.save()
Table2.save()
Table3.save()
Table4.save()
Table5.save()
Table6.save()
Table7.save() # This might fail. In case of failure I want to rollback saves from Table1 to Table6
transaction.savepoint_commit(sid)
except Exception as e:
print(str(e))
transaction.savepoint_rollback(sid)
return JsonResponse({"Status": 0, "Data": str(e)})
I've tried the above and Table7.save() has failed and Table1 to Table6 rollback has not happened.
I want to return the JSON response as {"Status": 0, Data: "Error That occurred"} in all the cases.
I don't want to re-raise the exception in except block as done in This link
What should I do to return a proper JSONResponse and rollback everything in case of failure?
As suggested in the link:
transaction.atomic will execute a transaction on the database if your
view produces a response without errors. Because you're catching the
exception yourself, it appears to Django that your view executed just
fine. If you catch the exception, you need to handle it yourself
An alternative approach is to use transaction.atomic inside with (as context manager), which will ensure a commit or rollback (again suggested on the same link and also explained here)
def my_view(request):
try:
with transaction.atomic():
Table1.save()
Table2.save()
Table3.save()
Table4.save()
Table5.save()
Table6.save()
Table7.save() # This might fail. In case of failure I want to rollback saves from Table1 to Table6
except Exception as e:
print(str(e))
return JsonResponse({"Status": 0, "Data": str(e)})
In my GAE application, I have several request handlers that return JSON-formated response. When one of these is called, if an error occurs (exception, or programming error), the output is not JSON: it is the stack trace.
What I need is:
Output without error:
{
"foo" : 1
"bar" : 2
"status" : "OK"
}
Output when an error occurs:
{
"status" : "ERR"
"errorMessage" : "An error occurred!"
}
My question is: What is the best practice to make sure that, in any case, the output will be a JSON-formated response? Of course, a common solution for every request handlers would be great.
Any help would be appreciated.
Sure - use the ereporter class (described here: https://stackoverflow.com/a/4296664/336505), but create a custom BaseHandler that formats your uncaught exceptions as JSON output:
class BaseHandler(webapp.RequestHandler):
def handle_exception(self, exception, debug_mode):
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(etc, etc) # format the exception
If an error occurs, to avoid getting a stack trace or other ugly output, you will need to employ a try ... except: http://docs.python.org/tutorial/errors.html
e.g.
try:
# ... your code ...
except TypeError as e:
# ... do something with this error type
except:
# ... generic exception catchall
# output that JSON response