Skip to content

Commit

Permalink
Introduce upb_EncodeStatus_String and upb_DecodeStatus_String
Browse files Browse the repository at this point in the history
Wrapper languages can now use these helpers to get human-friendly
error codes as opposed to manually re-mapping.

PiperOrigin-RevId: 634112262
  • Loading branch information
honglooker authored and copybara-github committed May 15, 2024
1 parent e71c811 commit 0645439
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1429,5 +1429,26 @@ upb_DecodeStatus upb_DecodeLengthPrefixed(const char* buf, size_t size,
return upb_Decode(buf, msg_len, msg, mt, extreg, options, arena);
}

const char* upb_DecodeStatus_String(upb_DecodeStatus status) {
switch (status) {
case kUpb_DecodeStatus_Ok:
return "Ok";
case kUpb_DecodeStatus_Malformed:
return "Wire format was corrupt";
case kUpb_DecodeStatus_OutOfMemory:
return "Arena alloc failed";
case kUpb_DecodeStatus_BadUtf8:
return "String field had bad UTF-8";
case kUpb_DecodeStatus_MaxDepthExceeded:
return "Exceeded upb_DecodeOptions_MaxDepth";
case kUpb_DecodeStatus_MissingRequired:
return "Missing required field";
case kUpb_DecodeStatus_UnlinkedSubMessage:
return "Unlinked sub-message field was present";
default:
return "Unknown decode status";
}
}

#undef OP_FIXPCK_LG2
#undef OP_VARPCK_LG2
3 changes: 3 additions & 0 deletions upb/wire/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ UPB_API upb_DecodeStatus upb_DecodeLengthPrefixed(
const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options,
upb_Arena* arena);

// Utility function for wrapper languages to get an error string from a
// upb_DecodeStatus.
UPB_API const char* upb_DecodeStatus_String(upb_DecodeStatus status);
#ifdef __cplusplus
} /* extern "C" */
#endif
Expand Down
15 changes: 15 additions & 0 deletions upb/wire/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,18 @@ upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
size_t* size) {
return _upb_Encode(msg, l, options, arena, buf, size, true);
}

const char* upb_EncodeStatus_String(upb_EncodeStatus status) {
switch (status) {
case kUpb_EncodeStatus_Ok:
return "Ok";
case kUpb_EncodeStatus_MissingRequired:
return "Missing required field";
case kUpb_EncodeStatus_MaxDepthExceeded:
return "Max depth exceeded";
case kUpb_EncodeStatus_OutOfMemory:
return "Arena alloc failed";
default:
return "Unknown encode status";
}
}
3 changes: 3 additions & 0 deletions upb/wire/encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
const upb_MiniTable* l,
int options, upb_Arena* arena,
char** buf, size_t* size);
// Utility function for wrapper languages to get an error string from a
// upb_EncodeStatus.
UPB_API const char* upb_EncodeStatus_String(upb_EncodeStatus status);

#ifdef __cplusplus
} /* extern "C" */
Expand Down

0 comments on commit 0645439

Please sign in to comment.