1

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 <listcomp> 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?

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.