Error handling

PROJ maintains an internal error state, which is local to a PJ_CONTEXT thread context.

See Quick start for more information about how to create and use a thread context object.

If you receive an abnormal return from a PROJ API function (e.g. a NULL pointer) you may wish to discover more information about the error.

In this case you can make a call to proj_context_errno(), passing in your thread context. This will return an integer error code.

If the error code is zero, the last PROJ operation was deemed successful and no error has been detected.

If the error code is non-zero, an error has been detected. You can pass your thread context together with this error code to proj_context_errno_string() to retrieve a string describing the error condition.

A basic example showing how a C program might catch and report errors follows:

errorhandling.c
 1  #include <stdio.h>
 2  #include <proj.h>
 3
 4  int main (void) {
 5      PJ_CONTEXT *c;
 6      PJ *p;
 7      int errno;
 8      const char *errstr;
 9
10      c = proj_context_create();
11      p = proj_create_crs_to_crs(c, "EPSG:4326", "EPSG:3857", NULL);
12
13      if (p == 0) {
14          /* Something is wrong, let's try to get details ... */
15          errno = proj_context_errno(c);
16          if (errno == 0) {
17              /* This should be impossible. */
18              fprintf(stderr, "Failed to create transformation, reason unknown.\n");
19          } else {
20              errstr = proj_context_errno_string(c, errno);
21              fprintf(stderr, "Failed to create transformation: %s.\n", errstr);
22          }
23          proj_context_destroy(c);
24          return 1;
25      }
26
27      /* transformation object is valid, do work ... */
28
29      proj_destroy(p);
30      proj_context_destroy(c);
31
32      return 0;
33  }