Skip to content

Commit bf4465b

Browse files
committed
Standardize error.cpp mappings.
1 parent 25245f1 commit bf4465b

File tree

1 file changed

+133
-122
lines changed

1 file changed

+133
-122
lines changed

src/error.cpp

Lines changed: 133 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ bool asio_is_canceled(const boost_code& ec) NOEXCEPT
288288
{
289289
// self termination
290290
return ec == boost_error_t::operation_canceled
291-
|| ec == boost::asio::error::operation_aborted;
291+
|| ec == asio_system_error_t::operation_aborted;
292292
}
293293

294294
// The success and operation_canceled codes are the only expected in normal
@@ -299,131 +299,144 @@ code asio_to_error_code(const boost_code& ec) NOEXCEPT
299299
if (!ec)
300300
return error::success;
301301

302-
// self termination
303-
if (ec == boost_error_t::connection_aborted ||
304-
ec == boost_error_t::operation_canceled)
305-
return error::operation_canceled;
306-
307-
// peer termination
308-
// stackoverflow.com/a/19891985/1172329
309-
if (ec == asio_misc_error_t::eof ||
310-
ec == boost_error_t::connection_reset)
311-
return error::peer_disconnect;
312-
313-
// learn.microsoft.com/en-us/troubleshoot/windows-client/networking/
314-
// connect-tcp-greater-than-5000-error-wsaenobufs-10055
315-
if (ec == asio_system_error_t::no_buffer_space)
316-
return error::invalid_configuration;
317-
318-
// network
319-
if (ec == boost_error_t::operation_not_permitted ||
320-
ec == boost_error_t::operation_not_supported ||
321-
ec == boost_error_t::owner_dead ||
322-
ec == boost_error_t::permission_denied)
323-
return error::not_allowed;
324-
325-
// connect-resolve
326-
if (ec == boost_error_t::address_family_not_supported ||
327-
ec == boost_error_t::bad_address ||
328-
ec == boost_error_t::destination_address_required ||
329-
ec == asio_netdb_error_t::host_not_found ||
330-
ec == asio_netdb_error_t::host_not_found_try_again)
331-
return error::resolve_failed;
332-
333-
// connect-connect
334-
if (ec == boost_error_t::address_not_available ||
335-
ec == boost_error_t::not_connected ||
336-
ec == boost_error_t::connection_refused ||
337-
ec == boost_error_t::broken_pipe ||
338-
ec == boost_error_t::host_unreachable ||
339-
ec == boost_error_t::network_down ||
340-
ec == boost_error_t::network_reset ||
341-
ec == boost_error_t::network_unreachable ||
342-
ec == boost_error_t::no_link ||
343-
ec == boost_error_t::no_protocol_option ||
344-
ec == boost_error_t::no_such_file_or_directory ||
345-
ec == boost_error_t::not_a_socket ||
346-
ec == boost_error_t::protocol_not_supported ||
347-
ec == boost_error_t::wrong_protocol_type)
348-
return error::connect_failed;
349-
350-
// connect-address
351-
if (ec == boost_error_t::address_in_use ||
352-
ec == boost_error_t::already_connected ||
353-
ec == boost_error_t::connection_already_in_progress ||
354-
ec == boost_error_t::operation_in_progress)
355-
return error::address_in_use;
356-
357-
// I/O (bad_file_descriptor if socket is not initialized)
358-
if (ec == boost_error_t::bad_file_descriptor ||
359-
ec == boost_error_t::bad_message ||
360-
ec == boost_error_t::illegal_byte_sequence ||
361-
ec == boost_error_t::io_error ||
362-
ec == boost_error_t::message_size ||
363-
ec == boost_error_t::no_message_available ||
364-
ec == boost_error_t::no_message ||
365-
ec == boost_error_t::no_stream_resources ||
366-
ec == boost_error_t::not_a_stream ||
367-
ec == boost_error_t::protocol_error)
368-
return error::bad_stream;
369-
370-
// timeout
371-
if (ec == boost_error_t::stream_timeout ||
372-
ec == boost_error_t::timed_out)
373-
return error::channel_timeout;
374-
375-
// file system errors (bad_file_descriptor used in I/O)
376-
if (ec == boost_error_t::cross_device_link ||
377-
ec == boost_error_t::device_or_resource_busy ||
378-
ec == boost_error_t::directory_not_empty ||
379-
ec == boost_error_t::executable_format_error ||
380-
ec == boost_error_t::file_exists ||
381-
ec == boost_error_t::file_too_large ||
382-
ec == boost_error_t::filename_too_long ||
383-
ec == boost_error_t::invalid_seek ||
384-
ec == boost_error_t::is_a_directory ||
385-
ec == boost_error_t::no_space_on_device ||
386-
ec == boost_error_t::no_such_device ||
387-
ec == boost_error_t::no_such_device_or_address ||
388-
ec == boost_error_t::read_only_file_system ||
389-
ec == boost_error_t::resource_unavailable_try_again ||
390-
ec == boost_error_t::text_file_busy ||
391-
ec == boost_error_t::too_many_files_open ||
392-
ec == boost_error_t::too_many_files_open_in_system ||
393-
ec == boost_error_t::too_many_links ||
394-
ec == boost_error_t::too_many_symbolic_link_levels)
395-
return error::file_system;
396-
397-
////// unknown
398-
////if (ec == boost_error_t::argument_list_too_long ||
399-
//// ec == boost_error_t::argument_out_of_domain ||
400-
//// ec == boost_error_t::function_not_supported ||
401-
//// ec == boost_error_t::identifier_removed ||
402-
//// ec == boost_error_t::inappropriate_io_control_operation ||
403-
//// ec == boost_error_t::interrupted ||
404-
//// ec == boost_error_t::invalid_argument ||
405-
//// ec == boost_error_t::no_buffer_space ||
406-
//// ec == boost_error_t::no_child_process ||
407-
//// ec == boost_error_t::no_lock_available ||
408-
//// ec == boost_error_t::no_such_process ||
409-
//// ec == boost_error_t::not_a_directory ||
410-
//// ec == boost_error_t::not_enough_memory ||
411-
//// ec == boost_error_t::operation_would_block ||
412-
//// ec == boost_error_t::resource_deadlock_would_occur ||
413-
//// ec == boost_error_t::result_out_of_range ||
414-
//// ec == boost_error_t::state_not_recoverable ||
415-
//// ec == boost_error_t::value_too_large)
302+
static const auto& system_category = boost::system::system_category();
303+
if (ec.category() == system_category)
304+
{
305+
switch (static_cast<boost_error_t>(ec.value()))
306+
{
307+
case boost_error_t::connection_aborted:
308+
case boost_error_t::operation_canceled:
309+
return error::operation_canceled;
310+
311+
// peer termination
312+
// stackoverflow.com/a/19891985/1172329
313+
case boost_error_t::connection_reset:
314+
return error::peer_disconnect;
315+
316+
// learn.microsoft.com/en-us/troubleshoot/windows-client/networking/
317+
// connect-tcp-greater-than-5000-error-wsaenobufs-10055
318+
case boost_error_t::no_buffer_space:
319+
return error::invalid_configuration;
320+
321+
// network
322+
case boost_error_t::operation_not_permitted:
323+
case boost_error_t::operation_not_supported:
324+
case boost_error_t::owner_dead:
325+
case boost_error_t::permission_denied:
326+
return error::not_allowed;
327+
328+
// connect-resolve
329+
case boost_error_t::address_family_not_supported:
330+
case boost_error_t::bad_address:
331+
case boost_error_t::destination_address_required:
332+
return error::resolve_failed;
333+
334+
// connect-connect
335+
case boost_error_t::address_not_available:
336+
case boost_error_t::not_connected:
337+
case boost_error_t::connection_refused:
338+
case boost_error_t::broken_pipe:
339+
case boost_error_t::host_unreachable:
340+
case boost_error_t::network_down:
341+
case boost_error_t::network_reset:
342+
case boost_error_t::network_unreachable:
343+
case boost_error_t::no_link:
344+
case boost_error_t::no_protocol_option:
345+
case boost_error_t::no_such_file_or_directory:
346+
case boost_error_t::not_a_socket:
347+
case boost_error_t::protocol_not_supported:
348+
case boost_error_t::wrong_protocol_type:
349+
return error::connect_failed;
350+
351+
// connect-address
352+
case boost_error_t::address_in_use:
353+
case boost_error_t::already_connected:
354+
case boost_error_t::connection_already_in_progress:
355+
case boost_error_t::operation_in_progress:
356+
return error::address_in_use;
357+
358+
// I/O (bad_file_descriptor if socket is not initialized)
359+
case boost_error_t::bad_file_descriptor:
360+
case boost_error_t::bad_message:
361+
case boost_error_t::illegal_byte_sequence:
362+
case boost_error_t::io_error:
363+
case boost_error_t::message_size:
364+
case boost_error_t::no_message_available:
365+
case boost_error_t::no_message:
366+
case boost_error_t::no_stream_resources:
367+
case boost_error_t::not_a_stream:
368+
case boost_error_t::protocol_error:
369+
return error::bad_stream;
370+
371+
// timeout
372+
case boost_error_t::stream_timeout:
373+
case boost_error_t::timed_out:
374+
return error::channel_timeout;
375+
376+
// file system errors (bad_file_descriptor used in I/O)
377+
case boost_error_t::cross_device_link:
378+
case boost_error_t::device_or_resource_busy:
379+
case boost_error_t::directory_not_empty:
380+
case boost_error_t::executable_format_error:
381+
case boost_error_t::file_exists:
382+
case boost_error_t::file_too_large:
383+
case boost_error_t::filename_too_long:
384+
case boost_error_t::invalid_seek:
385+
case boost_error_t::is_a_directory:
386+
case boost_error_t::no_space_on_device:
387+
case boost_error_t::no_such_device:
388+
case boost_error_t::no_such_device_or_address:
389+
case boost_error_t::read_only_file_system:
390+
case boost_error_t::resource_unavailable_try_again:
391+
case boost_error_t::text_file_busy:
392+
case boost_error_t::too_many_files_open:
393+
case boost_error_t::too_many_files_open_in_system:
394+
case boost_error_t::too_many_links:
395+
case boost_error_t::too_many_symbolic_link_levels:
396+
return error::file_system;
397+
default:
398+
return error::unknown;
399+
}
400+
}
401+
402+
static const auto& misc_category = boost::asio::error::get_misc_category();
403+
if (ec.category() == misc_category)
404+
{
405+
switch (static_cast<asio_misc_error_t>(ec.value()))
406+
{
407+
// peer termination
408+
// stackoverflow.com/a/19891985/1172329
409+
case asio_misc_error_t::eof:
410+
return error::peer_disconnect;
411+
default:
412+
return error::unknown;
413+
}
414+
}
415+
416+
static const auto& netdb_category = boost::asio::error::get_netdb_category();
417+
if (ec.category() == netdb_category)
418+
{
419+
switch (static_cast<asio_netdb_error_t>(ec.value()))
420+
{
421+
// connect-resolve
422+
case asio_netdb_error_t::host_not_found:
423+
case asio_netdb_error_t::host_not_found_try_again:
424+
return error::resolve_failed;
425+
default:
426+
return error::unknown;
427+
}
428+
}
429+
416430
return error::unknown;
417431
}
418432

419433
// includes json codes
420434
code http_to_error_code(const boost_code& ec) NOEXCEPT
421435
{
422-
static boost::beast::http::detail::http_error_category category{};
423-
424436
if (!ec)
425437
return error::success;
426438

439+
static const auto& category = boost::beast::http::detail::http_error_category();
427440
if (ec.category() != category)
428441
return json_to_error_code(ec);
429442

@@ -462,11 +475,10 @@ code http_to_error_code(const boost_code& ec) NOEXCEPT
462475
// includes json codes
463476
code ws_to_error_code(const boost_code& ec) NOEXCEPT
464477
{
465-
static boost::beast::websocket::detail::error_codes category{};
466-
467478
if (!ec)
468479
return error::success;
469480

481+
static boost::beast::websocket::detail::error_codes category{};
470482
if (ec.category() != category)
471483
return http_to_error_code(ec);
472484

@@ -509,11 +521,10 @@ code ws_to_error_code(const boost_code& ec) NOEXCEPT
509521

510522
code json_to_error_code(const boost_code& ec) NOEXCEPT
511523
{
512-
static boost::json::detail::error_code_category_t category{};
513-
514524
if (!ec)
515525
return error::success;
516526

527+
static boost::json::detail::error_code_category_t category{};
517528
if (ec.category() != category)
518529
return asio_to_error_code(ec);
519530

@@ -688,7 +699,7 @@ enum boost::asio::error::basic_errors
688699
};
689700

690701
// Boost: system error enum for error_conditions (platform-independent codes).
691-
enum boost::system::errc::errc_t
702+
enum boost::system::boost_error_t::boost_error_t
692703
{
693704
success = 0,
694705
address_family_not_supported = EAFNOSUPPORT,

0 commit comments

Comments
 (0)