<h4>Workaround</h4>
<p>You can cast all of your exceptions (and errors) to string.</p>
<p>Example</p>
<pre class="lang-py prettyprint-override"><code>    except Exception as e:
        logger.log_text(str(e))
        raise
</code></pre>
<p>This is still a strange workaround, I would have expected the module to have a better way for this. I tried <a href="https://github.com/googleapis/google-cloud-python/issues/6217" rel="nofollow noreferrer">StackDriver Logging python exception in protobuf json_format.py</a>
and it did not work. Any other ideas welcome.</p>

CC BY-SA 4.0
2022-02-10T14:16:02.993Z
2022-02-10T14:16:02.993Z
questionto42
<p>I changed the logging in a Google Cloud Function from the built-in Python module <code>logging</code> to <code>google.cloud.logging</code> and I cannot seem to export exceptions and error messages since they have the wrong type - expected string or bytes-like object.</p>
<p>A shortcut of the error:</p>
<blockquote>
<p>_ConvertFieldValuePair raise ParseError('Failed to parse {0} field: {1}.'.format(name, e)) google.protobuf.json_format.ParseError: Failed
to parse textPayload field: expected string or bytes-like object.</p>
</blockquote>
<p>The full error from the Google Cloud Logs:</p>
<blockquote>
<pre><code>&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py&quot;,
line 2073, in wsgi_app response = self.full_dispatch_request() File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py&quot;,
line 1518, in full_dispatch_request rv = self.handle_user_exception(e)
File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py&quot;,
line 1516, in full_dispatch_request rv = self.dispatch_request() File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py&quot;,
line 1502, in dispatch_request return
self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/__init__.py&quot;,
line 99, in view_func return function(request._get_current_object())
File &quot;/workspace/main.py&quot;, line 518, in get_sql_and_save_as_csv_in_gcs
store_duplicated_alids(conn_for_delete_insert, logger) File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/dryable/__init__.py&quot;,
line 34, in _decorated return function( * args, ** kwargs ) File
&quot;/workspace/main.py&quot;, line 368, in store_duplicated_alids
logger.log_text(e) File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/logger.py&quot;,
line 189, in log_text self._do_log(client, TextEntry, text, **kw) File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/logger.py&quot;,
line 158, in _do_log client.logging_api.write_entries([api_repr]) File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/_gapic.py&quot;,
line 151, in write_entries log_entry_pbs =
[_log_entry_mapping_to_pb(entry) for entry in entries] File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/_gapic.py&quot;,
line 151, in &lt;listcomp&gt; log_entry_pbs =
[_log_entry_mapping_to_pb(entry) for entry in entries] File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/_gapic.py&quot;,
line 542, in _log_entry_mapping_to_pb ParseDict(mapping, entry_pb)
File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/protobuf/json_format.py&quot;,
line 445, in ParseDict parser.ConvertMessage(js_dict, message) File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/protobuf/json_format.py&quot;,
line 476, in ConvertMessage self._ConvertFieldValuePair(value,
message) File
&quot;/layers/google.python.pip/pip/lib/python3.9/site-packages/google/protobuf/json_format.py&quot;,
line 594, in _ConvertFieldValuePair raise ParseError('Failed to parse
{0} field: {1}.'.format(name, e))
google.protobuf.json_format.ParseError: Failed to parse textPayload
field: expected string or bytes-like object.  ```
</code></pre>
</blockquote>
<p>How can I get rid of this error?</p>

<google-cloud-platform><logging>
".format(name, e)) ... google.protobuf.json_format.ParseError: Failed to parse textPayload field: expected string or bytes-like object." GCF logging
questionto42
# ".format(name, e)) ... google.protobuf.json_format.ParseError: Failed to parse textPayload field: expected string or bytes-like object." GCF logging

(Asked by: username_0 on 2022-02-10)

I changed the logging in a Google Cloud Function from the built-in Python module logging to google.cloud.logging and I cannot seem to export exceptions and error messages since they have the wrong type - expected string or bytes-like object.
A shortcut of the error:

_ConvertFieldValuePair raise ParseError('Failed to parse {0} field: {1}.'.format(name, e)) google.protobuf.json_format.ParseError: Failed
to parse textPayload field: expected string or bytes-like object.

The full error from the Google Cloud Logs:

"/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py",
line 2073, in wsgi_app response = self.full_dispatch_request() File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py",
line 1518, in full_dispatch_request rv = self.handle_user_exception(e)
File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py",
line 1516, in full_dispatch_request rv = self.dispatch_request() File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py",
line 1502, in dispatch_request return
self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/__init__.py",
line 99, in view_func return function(request._get_current_object())
File "/workspace/main.py", line 518, in get_sql_and_save_as_csv_in_gcs
store_duplicated_alids(conn_for_delete_insert, logger) File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/dryable/__init__.py",
line 34, in _decorated return function( * args, ** kwargs ) File
"/workspace/main.py", line 368, in store_duplicated_alids
logger.log_text(e) File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/logger.py",
line 189, in log_text self._do_log(client, TextEntry, text, **kw) File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/logger.py",
line 158, in _do_log client.logging_api.write_entries([api_repr]) File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/_gapic.py",
line 151, in write_entries log_entry_pbs =
[_log_entry_mapping_to_pb(entry) for entry in entries] File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/_gapic.py",
line 151, in  log_entry_pbs =
[_log_entry_mapping_to_pb(entry) for entry in entries] File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/cloud/logging_v2/_gapic.py",
line 542, in _log_entry_mapping_to_pb ParseDict(mapping, entry_pb)
File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/protobuf/json_format.py",
line 445, in ParseDict parser.ConvertMessage(js_dict, message) File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/protobuf/json_format.py",
line 476, in ConvertMessage self._ConvertFieldValuePair(value,
message) File
"/layers/google.python.pip/pip/lib/python3.9/site-packages/google/protobuf/json_format.py",
line 594, in _ConvertFieldValuePair raise ParseError('Failed to parse
{0} field: {1}.'.format(name, e))
google.protobuf.json_format.ParseError: Failed to parse textPayload
field: expected string or bytes-like object.  ```


How can I get rid of this error?

**Tags:** google-cloud-platform, logging


---

## 1 answers

### Answer by username_0 on 2022-02-10. Score: 2 | Accepted answer

Workaround
You can cast all of your exceptions (and errors) to string.
Example
    except Exception as e:
        logger.log_text(str(e))
        raise

This is still a strange workaround, I would have expected the module to have a better way for this. I tried StackDriver Logging python exception in protobuf json_format.py
and it did not work. Any other ideas welcome.


---

