# Error reporting in git

, , , , , and report errors of various kinds.

- is for failed internal assertions that should never happen, i.e. a bug in git itself.

- (lower-case, not ) is supposed to be used like but prints a "BUG" message instead of calling .

  A call to will then result in a "real" call to the function, either explicitly by invoking after call(s) to , or implicitly at time where we’ll check if we encountered any outstanding invocations.

  If there were no prior calls to before invoking the latter is a NOOP. The function takes the same arguments as itself. Calling explicitly isn’t necessary, but ensures that we die as soon as possible.

  If you know you had prior calls to then calling itself is equivalent to calling , the latter being a wrapper calling if we’ve set a flag indicating that we’ve called .

  This is for the convenience of APIs who’d like to potentially report more than one "bug", such as the optbug() validation in parse-options.c.

- is for fatal application errors. It prints a message to the user and exits with status 128.

- is for errors in command line usage. After printing its message, it exits with status 129. (See also in the parse-options API.)

- is for non-fatal library errors. It prints a message to the user and returns -1 for convenience in signaling the error to the caller.

- is for reporting situations that probably should not occur but which the user (and Git) can continue to work around without running into too many problems. Like , it returns -1 after reporting the situation to the caller.

These reports will be logged via the trace2 facility. See the "error" event in trace2 API.

## Customizable error handlers

The default behavior of and is to write a message to stderr and then exit or return as appropriate. This behavior can be overridden using and . For example, "git daemon" uses set_die_routine to write the reason was called to syslog before exiting.

## Library errors

Functions return a negative integer on error. Details beyond that vary from function to function:

- Some functions return -1 for all errors. Others return a more specific value depending on how the caller might want to react to the error.

- Some functions report the error to stderr with , while others leave that for the caller to do.

- errno is not meaningful on return from most functions (except for thin wrappers for system calls).

Check the function’s API documentation to be sure.

## Caller-handled errors

An increasing number of functions take a parameter struct strbuf *err. On error, such functions append a message about what went wrong to the err strbuf. The message is meant to be complete enough to be passed to or as-is. For example:

    if (ref_transaction_commit(transaction, &err))
            die("%s", err.buf);

The err parameter will be untouched if no error occurred, so multiple function calls can be chained:

    t = ref_transaction_begin(&err);
    if (!t ||
        ref_transaction_update(t, "HEAD", ..., &err) ||
        ret_transaction_commit(t, &err))
            die("%s", err.buf);

The err parameter must be a pointer to a valid strbuf. To silence a message, pass a strbuf that is explicitly ignored:

    if (thing_that_can_fail_in_an_ignorable_way(..., &err))
            /* This failure is okay. */
            strbuf_reset(&err);