pact-foundation / pact-reference

Reference implementations for the pact specifications
https://pact.io
MIT License
91 stars 46 forks source link

Inconsistent return for `get_error_message` #328

Closed JP-Ellis closed 10 months ago

JP-Ellis commented 11 months ago

While implementing the Pact FFI for Pact Python, I discovered that there is an inconsistency between the documented return value for the get_error_message FFI, and what it actually returns.

The documentation says that the return value is the number of bytes return to the buffer:

Return Codes

The number of bytes written to the provided buffer, which may be zero if there is no last error.

However an initial implementation returned 0, despite bytes having been written:

print("=> Sending invalid UTF-8 to validate_datetime")
invalid_utf8 = b"\xc3\x28"
ret: int = ffi.lib.pactffi_validate_datetime(invalid_utf8, invalid_utf8)
print(f"Ret: {ret!r}")

print("=> Getting error message")
length = 16 * 2**10
buffer = ffi.ffi.new("char[]", length)
ret = ffi.lib.pactffi_get_error_message(buffer, length)
print(f"Buffer: {buffer!r}")
print(f"Buffer content: {ffi.ffi.string(buffer)!r}")
print(f"Ret: {ret!r}")

Which produces the following output:

=> Sending invalid UTF-8 to validate_datetime
Ret: 2
=> Getting error message
Buffer: <cdata 'char[]' owning 16384 bytes>
Buffer content: b'error parsing value as UTF-8'
Ret: 0

I don't know whether this is an error in the implementation of the function, or in the documentation.