Skip to content

add H3_ERROR_END#1065

Merged
isaacbrodsky merged 5 commits into
uber:masterfrom
ajfriend:aj/error_max
Oct 15, 2025
Merged

add H3_ERROR_END#1065
isaacbrodsky merged 5 commits into
uber:masterfrom
ajfriend:aj/error_max

Conversation

@ajfriend
Copy link
Copy Markdown
Collaborator

Define H3_ERROR_CODE_MAX in h3api.h.in to define the range of errors. Keep it updated as we add new errors.

Idea: Use this to address @dfellis's comment here: #1063 (comment)

@coveralls
Copy link
Copy Markdown

coveralls commented Oct 15, 2025

Coverage Status

coverage: 98.936%. remained the same
when pulling bbf4456 on ajfriend:aj/error_max
into b29d820 on uber:master.

@ajfriend
Copy link
Copy Markdown
Collaborator Author

Alternatively, AI suggested something like this:

typedef enum {
    E_SUCCESS = 0,
    E_FAILED,
    E_DOMAIN,
    /* ... */
    E_DELETED_DIGIT,
    E_COUNT  // sentinel for iteration and sizing
} H3ErrorCodes;

#define H3_ERROR_MAX (E_COUNT - 1)

Comment thread src/h3lib/lib/h3Index.c Outdated
@justinhwang
Copy link
Copy Markdown
Contributor

Alternatively, AI suggested something like this:

typedef enum {
    E_SUCCESS = 0,
    E_FAILED,
    E_DOMAIN,
    /* ... */
    E_DELETED_DIGIT,
    E_COUNT  // sentinel for iteration and sizing
} H3ErrorCodes;

#define H3_ERROR_MAX (E_COUNT - 1)

We could use E_COUNT directly then no? (I also had a similar idea when adding E_INDEX_INVALID but wasn't sure as then it can be referenced externally - though maybe people would want a similar constant)

@ajfriend
Copy link
Copy Markdown
Collaborator Author

@isaacbrodsky, any thoughts on which approach you'd prefer?

@ajfriend
Copy link
Copy Markdown
Collaborator Author

ajfriend commented Oct 15, 2025

Alternatively, AI suggested something like this:

typedef enum {
    E_SUCCESS = 0,
    E_FAILED,
    E_DOMAIN,
    /* ... */
    E_DELETED_DIGIT,
    E_COUNT  // sentinel for iteration and sizing
} H3ErrorCodes;

#define H3_ERROR_MAX (E_COUNT - 1)

We could use E_COUNT directly then no? (I also had a similar idea when adding E_INDEX_INVALID but wasn't sure as then it can be referenced externally - though maybe people would want a similar constant)

Yeah, I suppose we could do something as simple as:

typedef enum {
    E_SUCCESS = 0,
    E_FAILED = 1,
    E_DOMAIN = 2,
    /* ... */
    E_OPTION_INVALID = 15,  // Mode or flags argument was not valid
    E_INDEX_INVALID = 16,    // `H3Index` argument was not valid
    H3_ERROR_SENTINEL  // sentinel for iteration and sizing
} H3ErrorCodes;

@isaacbrodsky
Copy link
Copy Markdown
Collaborator

@isaacbrodsky, any thoughts on which approach you'd prefer?

We do something similar here:

NUM_DIGITS = INVALID_DIGIT,
I think whichever way you define the max sentinel is fine. I do prefer having the values explicitly set in the enum declaration, because I expect the code will be read much more than it will be written, and so I want to make reading it as easy as possible.

@dfellis
Copy link
Copy Markdown
Collaborator

dfellis commented Oct 15, 2025

Alternatively, AI suggested something like this:

typedef enum {
    E_SUCCESS = 0,
    E_FAILED,
    E_DOMAIN,
    /* ... */
    E_DELETED_DIGIT,
    E_COUNT  // sentinel for iteration and sizing
} H3ErrorCodes;

#define H3_ERROR_MAX (E_COUNT - 1)

We could use E_COUNT directly then no? (I also had a similar idea when adding E_INDEX_INVALID but wasn't sure as then it can be referenced externally - though maybe people would want a similar constant)

Yeah, I suppose we could do something as simple as:

typedef enum {
    E_SUCCESS = 0,
    E_FAILED = 1,
    E_DOMAIN = 2,
    /* ... */
    E_OPTION_INVALID = 15,  // Mode or flags argument was not valid
    E_INDEX_INVALID = 16,    // `H3Index` argument was not valid
    H3_ERROR_SENTINEL  // sentinel for iteration and sizing
} H3ErrorCodes;

This last one looks the "best" to me because (1) it's obviously different from the rest because it doesn't get a number assigned and it doesn't start with E_ and (2) it will automatically be updated when new error codes are added without requiring that the developer remembers to do so (I doubt it would happen if anyone involved with H3 right now would do so, but in a couple of years a new contributor might not know and we might not remember if we don't explicitly see the sentinel definition in the Github diff view).

It could be made even more explicit by putting an inline multi-line comment above the H3_ERROR_SENTINEL stating something like:

/**
 * End of H3 Error Codes. Below is the sentinel value used for
 * iteration and sizing of the enum. Do not add error codes below it.
 **/

And if we want to be extra clever, the enum could be named H3_ERROR_LENGTH and then we still #define H3_ERROR_MAX (H3_ERROR_LENGTH - 1) below it so we don't have to have the subtraction in the code where the constant is used.

@ajfriend ajfriend requested a review from isaacbrodsky October 15, 2025 18:53
@ajfriend ajfriend changed the title add H3_ERROR_CODE_MAX add H3_ERROR_END Oct 15, 2025
@isaacbrodsky isaacbrodsky merged commit 5081142 into uber:master Oct 15, 2025
47 of 48 checks passed
@ajfriend ajfriend deleted the aj/error_max branch October 15, 2025 20:45
@ajfriend ajfriend added this to the v4.4 milestone Oct 16, 2025
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.

5 participants