Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add canonical error codes for generic error handling. #2711

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions toxcore/tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,109 @@ uint32_t tox_max_hostname_length(void);

/** @} */

/** @{
* @name Canonical error codes.
*/

/**
* @brief List of canonical error codes.
*
* All function-specific error codes have a mapping to canonical codes. These
* can be useful to implement generic error handling.
*/
typedef enum Tox_Err {

/**
* Operation was successful.
*/
TOX_ERR_OK,

/**
* An error occurred but it's unclear how to map it to any of the
* canonical codes.
*
* Not retriable.
*/
TOX_ERR_UNKNOWN,

/**
* A function parameter had an invalid format. E.g. host names, tox save
* data, port numbers, etc. or NULL was passed where non-NULL was expected.
*
* Not retriable.
*/
TOX_ERR_INVALID_ARGUMENT,

/**
* A numeric input was outside the valid range. E.g. friend numbers. Could
* be valid later if a friend with that number is added. INVALID_ARGUMENT
* is issued when the input passed can never be valid. OUT_OF_RANGE is
* issued when it's only currently not valid.
*
* Retriable if the system state has changed.
*/
TOX_ERR_OUT_OF_RANGE,

/**
* The system state is not ready for the operation to be performed.
*
* Retriable if the system state has changed.
*/
TOX_ERR_FAILED_PRECONDITION,

/**
* A non-memory resource allocation failed (e.g. sockets).
*
* Retriable, but not likely to succeed.
*/
TOX_ERR_RESOURCE_EXHAUSTED,

/**
* The function failed to allocate enough memory for the operation. This
* is special-cased from `RESOURCE_EXHAUSTED`, because it's more likely
* that the client can do something about it (e.g. free some memory).
*
* Retriable.
*/
TOX_ERR_MALLOC,

/**
* An object requested (group, friend, hostname, etc.) does not exist.
*
* Not retriable.
*/
TOX_ERR_NOT_FOUND,

/**
* The entity we're trying to create (or add, e.g. a friend) already
* exists.
*
* Not retriable.
*/
TOX_ERR_ALREADY_EXISTS,

/**
* The caller's role does not have the permission to perform the action.
*
* Not retriable.
*/
TOX_ERR_PERMISSION_DENIED,

/**
* Packet send failed or some other intermittent failure made the operation
* fail. Retrying is likely to eventually succeed. This could also mean the
* target (e.g. friend) is offline.
*
* Retriable.
*/
TOX_ERR_UNAVAILABLE,

} Tox_Err;

const char *tox_err_to_string(Tox_Err value);

/** @} */

/** @{
* @name Global enumerations
*/
Expand Down Expand Up @@ -795,6 +898,7 @@ typedef enum Tox_Err_Options_New {
} Tox_Err_Options_New;

const char *tox_err_options_new_to_string(Tox_Err_Options_New value);
Tox_Err tox_err_options_new_to_err(Tox_Err_Options_New value);

/**
* @brief Allocates a new Tox_Options object and initialises it with the default
Expand Down Expand Up @@ -887,6 +991,7 @@ typedef enum Tox_Err_New {
} Tox_Err_New;

const char *tox_err_new_to_string(Tox_Err_New value);
Tox_Err tox_err_new_to_err(Tox_Err_New value);

/**
* @brief Creates and initialises a new Tox instance with the options passed.
Expand Down
49 changes: 49 additions & 0 deletions toxcore/tox_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1721,3 +1721,52 @@

return "<invalid Tox_Group_Mod_Event>";
}

Tox_Err tox_err_options_new_to_err(Tox_Err_Options_New value)
{
switch (value) {
case TOX_ERR_OPTIONS_NEW_OK:
return TOX_ERR_OK;

Check warning on line 1729 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1727-L1729

Added lines #L1727 - L1729 were not covered by tests

case TOX_ERR_OPTIONS_NEW_MALLOC:
return TOX_ERR_MALLOC;

Check warning on line 1732 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1731-L1732

Added lines #L1731 - L1732 were not covered by tests
}

return TOX_ERR_UNKNOWN;

Check warning on line 1735 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1735

Added line #L1735 was not covered by tests
}
Tox_Err tox_err_new_to_err(Tox_Err_New value)
{
switch (value) {
case TOX_ERR_NEW_OK:
return TOX_ERR_OK;

Check warning on line 1741 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1739-L1741

Added lines #L1739 - L1741 were not covered by tests

case TOX_ERR_NEW_NULL:
return TOX_ERR_INVALID_ARGUMENT;

Check warning on line 1744 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1743-L1744

Added lines #L1743 - L1744 were not covered by tests

case TOX_ERR_NEW_MALLOC:
return TOX_ERR_MALLOC;

Check warning on line 1747 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1746-L1747

Added lines #L1746 - L1747 were not covered by tests

case TOX_ERR_NEW_PORT_ALLOC:
return TOX_ERR_RESOURCE_EXHAUSTED;

Check warning on line 1750 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1749-L1750

Added lines #L1749 - L1750 were not covered by tests

case TOX_ERR_NEW_PROXY_BAD_TYPE:
return TOX_ERR_INVALID_ARGUMENT;

Check warning on line 1753 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1752-L1753

Added lines #L1752 - L1753 were not covered by tests

case TOX_ERR_NEW_PROXY_BAD_HOST:
return TOX_ERR_INVALID_ARGUMENT;

Check warning on line 1756 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1755-L1756

Added lines #L1755 - L1756 were not covered by tests

case TOX_ERR_NEW_PROXY_BAD_PORT:
return TOX_ERR_INVALID_ARGUMENT;

Check warning on line 1759 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1758-L1759

Added lines #L1758 - L1759 were not covered by tests

case TOX_ERR_NEW_PROXY_NOT_FOUND:
return TOX_ERR_NOT_FOUND;

Check warning on line 1762 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1761-L1762

Added lines #L1761 - L1762 were not covered by tests

case TOX_ERR_NEW_LOAD_ENCRYPTED:
return TOX_ERR_INVALID_ARGUMENT;

Check warning on line 1765 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1764-L1765

Added lines #L1764 - L1765 were not covered by tests

case TOX_ERR_NEW_LOAD_BAD_FORMAT:
return TOX_ERR_INVALID_ARGUMENT;

Check warning on line 1768 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1767-L1768

Added lines #L1767 - L1768 were not covered by tests
}

return TOX_ERR_UNKNOWN;

Check warning on line 1771 in toxcore/tox_api.c

View check run for this annotation

Codecov / codecov/patch

toxcore/tox_api.c#L1771

Added line #L1771 was not covered by tests
}
Loading