Skip to content

Commit 3fd6a4e

Browse files
committed
[FIX] api_log: Log readonly exception
1 parent 4bd47a7 commit 3fd6a4e

11 files changed

Lines changed: 129 additions & 45 deletions

File tree

api_log/README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.. image:: https://odoo-community.org/readme-banner-image
2+
:target: https://odoo-community.org/get-involved?utm_source=readme
3+
:alt: Odoo Community Association
4+
15
=======
26
API Log
37
=======
@@ -13,7 +17,7 @@ API Log
1317
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1418
:target: https://odoo-community.org/page/development-status
1519
:alt: Beta
16-
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
20+
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
1721
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1822
:alt: License: AGPL-3
1923
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github

api_log/models/api_log.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ def log_response(self, response):
173173
return self.sudo().write(log_response_values)
174174

175175
def _prepare_log_exception(self, exception):
176-
exception.headers = getattr(exception, "headers", {})
176+
if not hasattr(exception, "headers"):
177+
exception.headers = dict()
177178
values = {
178179
"stack_trace": "".join(format_exception(exception)),
179180
"response_headers": self._inject_log_entry(exception.headers),

api_log/static/description/index.html

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>API Log</title>
6+
<title>README.rst</title>
77
<style type="text/css">
88

99
/*
@@ -360,16 +360,21 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document" id="api-log">
364-
<h1 class="title">API Log</h1>
363+
<div class="document">
365364

365+
366+
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367+
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368+
</a>
369+
<div class="section" id="api-log">
370+
<h1>API Log</h1>
366371
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
367372
!! This file is generated by oca-gen-addon-readme !!
368373
!! changes will be overwritten. !!
369374
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370375
!! source digest: sha256:ef0c0bceb8ae27bcfebaebc22e2fb4747475f2a2c60dd2d410bc40b6efee9b6a
371376
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/16.0/api_log"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-api_log"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
377+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/16.0/api_log"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-api_log"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373378
<p>This module allows to store request and response logs for any API.</p>
374379
<p>When a response is logged, the header <tt class="docutils literal">API_LOG_ENTRY_ID</tt> is injected
375380
in the response header. This header stores the identifier of the log
@@ -387,23 +392,23 @@ <h1 class="title">API Log</h1>
387392
</ul>
388393
</div>
389394
<div class="section" id="bug-tracker">
390-
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
395+
<h2><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h2>
391396
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>.
392397
In case of trouble, please check there if your issue has already been reported.
393398
If you spotted it first, help us to smash it by providing a detailed and welcomed
394399
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20api_log%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
395400
<p>Do not contact contributors directly about support or help with technical issues.</p>
396401
</div>
397402
<div class="section" id="credits">
398-
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
403+
<h2><a class="toc-backref" href="#toc-entry-2">Credits</a></h2>
399404
<div class="section" id="authors">
400-
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
405+
<h3><a class="toc-backref" href="#toc-entry-3">Authors</a></h3>
401406
<ul class="simple">
402407
<li>Akretion</li>
403408
</ul>
404409
</div>
405410
<div class="section" id="contributors">
406-
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
411+
<h3><a class="toc-backref" href="#toc-entry-4">Contributors</a></h3>
407412
<ul class="simple">
408413
<li>Florian Mounier <a class="reference external" href="mailto:florian.mounier&#64;akretion.com">florian.mounier&#64;akretion.com</a></li>
409414
<li>Guewen Baconnier <a class="reference external" href="mailto:guewen.baconnier&#64;camptocamp.com">guewen.baconnier&#64;camptocamp.com</a></li>
@@ -415,7 +420,7 @@ <h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
415420
</ul>
416421
</div>
417422
<div class="section" id="maintainers">
418-
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
423+
<h3><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h3>
419424
<p>This module is maintained by the OCA.</p>
420425
<a class="reference external image-reference" href="https://odoo-community.org">
421426
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -430,5 +435,6 @@ <h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
430435
</div>
431436
</div>
432437
</div>
438+
</div>
433439
</body>
434440
</html>

api_log/tests/common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
# Copyright 2025 Simone Rubino - PyTech
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33

4+
import contextlib
5+
6+
from odoo.http import Response
47
from odoo.tests.common import HttpCase
58

9+
from odoo.addons.website.tools import MockRequest
10+
611

712
class Common(HttpCase):
813
@classmethod
914
def setUpClass(cls):
1015
super().setUpClass()
1116
cls.log_model = cls.env["api.log"]
17+
18+
@contextlib.contextmanager
19+
def _mock_request_exc_handling(self, *args, **kwargs):
20+
"""Enhance the standard mock of a request with exception handling."""
21+
with MockRequest(*args, **kwargs) as mock_request:
22+
mock_request.dispatcher.handle_error = lambda exc: Response()
23+
yield mock_request

api_log/tests/test_api_log.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ def test_log_response(self):
5050

5151
def test_log_exception(self):
5252
log = self.log_model.create({})
53-
log.log_exception(Exception())
53+
54+
with self._mock_request_exc_handling(self.env):
55+
log.log_exception(Exception())
5456

5557
self.assertEqual(log.response_headers["API-Log-Entry-ID"], str(log.id))
58+
59+
def test_log_exception_readonly_headers(self):
60+
"""
61+
If the exception's headers are readonly,
62+
they can be logged.
63+
"""
64+
# Arrange
65+
log = self.log_model.create({})
66+
exc_headers = {
67+
"answer": 42,
68+
}
69+
70+
class ReadOnlyException(Exception):
71+
@property
72+
def headers(self):
73+
return exc_headers.copy()
74+
75+
ro_exception = ReadOnlyException()
76+
# pre-condition
77+
with self.assertRaises(AttributeError) as ae:
78+
ro_exception.headers = dict()
79+
self.assertIn("can't set attribute", str(ae.exception))
80+
81+
# Act
82+
with self._mock_request_exc_handling(self.env):
83+
log.log_exception(ro_exception)
84+
85+
# Assert
86+
self.assertLess(exc_headers.items(), log.response_headers.items())

api_log_mail/README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.. image:: https://odoo-community.org/readme-banner-image
2+
:target: https://odoo-community.org/get-involved?utm_source=readme
3+
:alt: Odoo Community Association
4+
15
====================
26
API Log notification
37
====================
@@ -13,7 +17,7 @@ API Log notification
1317
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1418
:target: https://odoo-community.org/page/development-status
1519
:alt: Beta
16-
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
20+
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
1721
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1822
:alt: License: AGPL-3
1923
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github

api_log_mail/static/description/index.html

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>API Log notification</title>
6+
<title>README.rst</title>
77
<style type="text/css">
88

99
/*
@@ -360,16 +360,21 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document" id="api-log-notification">
364-
<h1 class="title">API Log notification</h1>
363+
<div class="document">
365364

365+
366+
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367+
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368+
</a>
369+
<div class="section" id="api-log-notification">
370+
<h1>API Log notification</h1>
366371
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
367372
!! This file is generated by oca-gen-addon-readme !!
368373
!! changes will be overwritten. !!
369374
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370375
!! source digest: sha256:ef0c0bceb8ae27bcfebaebc22e2fb4747475f2a2c60dd2d410bc40b6efee9b6a
371376
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/16.0/api_log_mail"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-api_log_mail"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
377+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/16.0/api_log_mail"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-api_log_mail"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373378
<p>This module allows to create an activity when an exception is logged in
374379
an API logs collection.</p>
375380
<p><strong>Table of contents</strong></p>
@@ -386,28 +391,28 @@ <h1 class="title">API Log notification</h1>
386391
</ul>
387392
</div>
388393
<div class="section" id="configuration">
389-
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
394+
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
390395
<p>In any log collection that has logging enabled, insert an activity type
391396
in “Error Activity type”.</p>
392397
</div>
393398
<div class="section" id="bug-tracker">
394-
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
399+
<h2><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h2>
395400
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>.
396401
In case of trouble, please check there if your issue has already been reported.
397402
If you spotted it first, help us to smash it by providing a detailed and welcomed
398403
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20api_log_mail%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
399404
<p>Do not contact contributors directly about support or help with technical issues.</p>
400405
</div>
401406
<div class="section" id="credits">
402-
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
407+
<h2><a class="toc-backref" href="#toc-entry-3">Credits</a></h2>
403408
<div class="section" id="authors">
404-
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
409+
<h3><a class="toc-backref" href="#toc-entry-4">Authors</a></h3>
405410
<ul class="simple">
406411
<li>PyTech</li>
407412
</ul>
408413
</div>
409414
<div class="section" id="contributors">
410-
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
415+
<h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
411416
<ul class="simple">
412417
<li><a class="reference external" href="https://www.pytech.it">PyTech</a>:<ul>
413418
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;pytech.it">simone.rubino&#64;pytech.it</a>&gt;</li>
@@ -416,7 +421,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
416421
</ul>
417422
</div>
418423
<div class="section" id="maintainers">
419-
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
424+
<h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
420425
<p>This module is maintained by the OCA.</p>
421426
<a class="reference external image-reference" href="https://odoo-community.org">
422427
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -431,5 +436,6 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
431436
</div>
432437
</div>
433438
</div>
439+
</div>
434440
</body>
435441
</html>

fastapi_log/README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.. image:: https://odoo-community.org/readme-banner-image
2+
:target: https://odoo-community.org/get-involved?utm_source=readme
3+
:alt: Odoo Community Association
4+
15
===========
26
Fastapi Log
37
===========
@@ -13,7 +17,7 @@ Fastapi Log
1317
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1418
:target: https://odoo-community.org/page/development-status
1519
:alt: Beta
16-
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
20+
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
1721
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1822
:alt: License: AGPL-3
1923
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github

0 commit comments

Comments
 (0)