reftable/error: discern locked/outdated errors

We currently throw two different errors into a similar-but-different error code: - Errors when trying to lock the reftable stack. - Errors when trying to write to the reftable stack which has been modified concurrently. This results in unclear error handling and user-visible error messages. Create a new `REFTABLE_OUTDATED_ERROR` so that those error conditions can be clearly told apart from each other. Adjust users of the old `REFTABLE_LOCK_ERROR` to use the new error code as required. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 25, 2024 at 11:02 UTC af18098c9d2b2e165aca127c35eeb98d157bd542
4 files changed +11 -6
reftable/error.c
+3 -1
@@ -22,7 +22,7 @@ const char *reftable_error_str(int err)
22 case REFTABLE_NOT_EXIST_ERROR:
23 return "file does not exist";
24 case REFTABLE_LOCK_ERROR:
25 - return "data is outdated";
25 + return "data is locked";
26 case REFTABLE_API_ERROR:
27 return "misuse of the reftable API";
28 case REFTABLE_ZLIB_ERROR:
@@ -35,6 +35,8 @@ const char *reftable_error_str(int err)
35 return "invalid refname";
36 case REFTABLE_ENTRY_TOO_BIG_ERROR:
37 return "entry too large";
38 + case REFTABLE_OUTDATED_ERROR:
39 + return "data concurrently modified";
40 case -1:
41 return "general error";
42 default:
reftable/reftable-error.h
+4 -1
@@ -25,7 +25,7 @@ enum reftable_error {
25 */
26 REFTABLE_NOT_EXIST_ERROR = -4,
27
28 - /* Trying to write out-of-date data. */
28 + /* Trying to access locked data. */
29 REFTABLE_LOCK_ERROR = -5,
30
31 /* Misuse of the API:
@@ -57,6 +57,9 @@ enum reftable_error {
57 /* Entry does not fit. This can happen when writing outsize reflog
58 messages. */
59 REFTABLE_ENTRY_TOO_BIG_ERROR = -11,
60 +
61 + /* Trying to write out-of-date data. */
62 + REFTABLE_OUTDATED_ERROR = -12,
63 };
64
65 /* convert the numeric error code to a string. The string should not be
reftable/stack.c
+3 -3
@@ -529,9 +529,9 @@ int reftable_stack_add(struct reftable_stack *st,
529 {
530 int err = stack_try_add(st, write, arg);
531 if (err < 0) {
532 - if (err == REFTABLE_LOCK_ERROR) {
532 + if (err == REFTABLE_OUTDATED_ERROR) {
533 /* Ignore error return, we want to propagate
534 - REFTABLE_LOCK_ERROR.
534 + REFTABLE_OUTDATED_ERROR.
535 */
536 reftable_stack_reload(st);
537 }
@@ -591,7 +591,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
591 if (err < 0)
592 goto done;
593 if (err > 0) {
594 - err = REFTABLE_LOCK_ERROR;
594 + err = REFTABLE_OUTDATED_ERROR;
595 goto done;
596 }
597
reftable/stack_test.c
+1 -1
@@ -232,7 +232,7 @@ static void test_reftable_stack_uptodate(void)
232 EXPECT_ERR(err);
233
234 err = reftable_stack_add(st2, &write_test_ref, &ref2);
235 - EXPECT(err == REFTABLE_LOCK_ERROR);
235 + EXPECT(err == REFTABLE_OUTDATED_ERROR);
236
237 err = reftable_stack_reload(st2);
238 EXPECT_ERR(err);