Skip to content

Remove C++ exceptions from SystemTimeFormatter and derived classes#45372

Open
yanavlasov wants to merge 2 commits into
envoyproxy:mainfrom
yanavlasov:remove-exceptions-1
Open

Remove C++ exceptions from SystemTimeFormatter and derived classes#45372
yanavlasov wants to merge 2 commits into
envoyproxy:mainfrom
yanavlasov:remove-exceptions-1

Conversation

@yanavlasov
Copy link
Copy Markdown
Contributor

@yanavlasov yanavlasov commented Jun 1, 2026

Replace constructors that can throw with a factory method that returns StatusOr<> for SystemTimeFormatter and derived classes. There should be no functional changes, as there is still a throw present when a factory method fails.

Test changes are all conversions from direct construction to a factory method.

In the subsequent PRs I will generalize the makeTimeFormatter so it can be used with generic types.

There are boilerplate checkConstructPreconditions method in the derived classes that just call the base class. They are not strictly needed, and I have left them, just to so I can use this as pattern for agents that will work on implementing this pattern for other types where derived classes have their own preconditions.

Risk Level: low
Testing: unit tests
Docs Changes: no
Release Notes: no
Platform Specific Features: no

Signed-off-by: Yan Avlasov <yavlasov@google.com>
@yanavlasov
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors SystemTimeFormatter and its derived classes to use factory methods returning absl::StatusOr instead of throwing exceptions during construction. The reviewer feedback suggests removing the redundant checkConstructPreconditions static methods from all derived classes, as they can inherit this behavior directly from SystemTimeFormatter and be accessed via the friended makeTimeFormatter factory function.

@envoyproxy envoyproxy deleted a comment from gemini-code-assist Bot Jun 1, 2026
@envoyproxy envoyproxy deleted a comment from gemini-code-assist Bot Jun 1, 2026
@envoyproxy envoyproxy deleted a comment from gemini-code-assist Bot Jun 1, 2026
@envoyproxy envoyproxy deleted a comment from gemini-code-assist Bot Jun 1, 2026
@envoyproxy envoyproxy deleted a comment from gemini-code-assist Bot Jun 1, 2026
Copy link
Copy Markdown
Member

@wbpcode wbpcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall except one comment. Thanks, Yan, for working on this.

absl::StatusOr<std::unique_ptr<T>> makeTimeFormatter(absl::string_view format) {
static_assert(std::is_base_of<SystemTimeFormatter, T>::value,
"T must be derived from SystemTimeFormatter");
RETURN_IF_NOT_OK(T::checkConstructPreconditions(format));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need to add new checkConstructPreconditions() to every derived class because seems they are completely same?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have removed them.

Signed-off-by: Yan Avlasov <yavlasov@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants