Skip to content

Commit e1e3ab7

Browse files
committed
net/Resolver: throw std::system_error with a new error_category
1 parent 3f0c4ed commit e1e3ab7

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/net/Resolver.cxx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "AddressInfo.hxx"
77
#include "HostParser.hxx"
88
#include "lib/fmt/RuntimeError.hxx"
9+
#include "lib/fmt/ToBuffer.hxx"
910
#include "util/CharUtil.hxx"
1011
#include "util/StringAPI.hxx"
1112

@@ -21,23 +22,35 @@
2122

2223
#include <stdio.h>
2324

25+
ResolverErrorCategory resolver_error_category;
26+
27+
std::string
28+
ResolverErrorCategory::message(int condition) const
29+
{
30+
#ifdef _WIN32
31+
return gai_strerrorA(condition);
32+
#else
33+
return gai_strerror(condition);
34+
#endif
35+
}
36+
37+
static std::system_error
38+
MakeResolverError(int error, const char *msg) noexcept
39+
{
40+
return std::system_error{error, resolver_error_category, msg};
41+
}
42+
2443
AddressInfoList
2544
Resolve(const char *node, const char *service,
2645
const struct addrinfo *hints)
2746
{
2847
struct addrinfo *ai;
2948
int error = getaddrinfo(node, service, hints, &ai);
30-
if (error != 0) {
31-
#ifdef _WIN32
32-
const char *msg = gai_strerrorA(error);
33-
#else
34-
const char *msg = gai_strerror(error);
35-
#endif
36-
throw FmtRuntimeError("Failed to resolve {:?}:{:?}: {}",
37-
node == nullptr ? "" : node,
38-
service == nullptr ? "" : service,
39-
msg);
40-
}
49+
if (error != 0)
50+
throw MakeResolverError(error,
51+
FmtBuffer<512>("Failed to resolve {:?}:{:?}",
52+
node == nullptr ? "" : node,
53+
service == nullptr ? "" : service).c_str());
4154

4255
return AddressInfoList(ai);
4356
}

src/net/Resolver.hxx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,27 @@
44

55
#pragma once
66

7+
#include <system_error>
8+
79
class AddressInfoList;
810

11+
class ResolverErrorCategory final : public std::error_category {
12+
public:
13+
const char *name() const noexcept override {
14+
return "gai";
15+
}
16+
17+
std::string message(int condition) const override;
18+
};
19+
20+
extern ResolverErrorCategory resolver_error_category;
21+
922
/**
1023
* Thin wrapper for getaddrinfo() which throws on error and returns a
1124
* RAII object.
25+
*
26+
* getaddrinfo() errors are thrown as std::system_error with
27+
* #resolver_error_category.
1228
*/
1329
AddressInfoList
1430
Resolve(const char *node, const char *service,
@@ -21,7 +37,8 @@ Resolve(const char *node, const char *service,
2137
* This is a wrapper for getaddrinfo() and it does not support local
2238
* sockets.
2339
*
24-
* Throws on error.
40+
* Throws on error. Resolver errors are thrown as std::system_error
41+
* with #resolver_error_category.
2542
*/
2643
AddressInfoList
2744
Resolve(const char *host_and_port, int default_port,

0 commit comments

Comments
 (0)