diff options
author | David Robillard <d@drobilla.net> | 2023-05-05 10:34:37 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | febb30141356df83a56026bbb14099e3cb2c82fc (patch) | |
tree | f1d2abcdc3872e375ffe8e4138a8043c20b3b1a8 | |
parent | b739848b6ea4c038610ceb7c0b00984a86d99fa9 (diff) | |
download | serd-febb30141356df83a56026bbb14099e3cb2c82fc.tar.gz serd-febb30141356df83a56026bbb14099e3cb2c82fc.tar.bz2 serd-febb30141356df83a56026bbb14099e3cb2c82fc.zip |
Use more human-readable status codes
-rw-r--r-- | include/serd/env.h | 4 | ||||
-rw-r--r-- | include/serd/status.h | 32 | ||||
-rw-r--r-- | src/byte_sink.h | 2 | ||||
-rw-r--r-- | src/byte_source.c | 2 | ||||
-rw-r--r-- | src/byte_source.h | 2 | ||||
-rw-r--r-- | src/env.c | 10 | ||||
-rw-r--r-- | src/n3.c | 154 | ||||
-rw-r--r-- | src/reader.c | 8 | ||||
-rw-r--r-- | src/reader.h | 4 | ||||
-rw-r--r-- | src/serdi.c | 2 | ||||
-rw-r--r-- | src/string.c | 46 | ||||
-rw-r--r-- | src/writer.c | 20 | ||||
-rw-r--r-- | test/test_reader_writer.c | 2 | ||||
-rw-r--r-- | test/test_string.c | 2 | ||||
-rw-r--r-- | test/test_writer.c | 6 |
15 files changed, 157 insertions, 139 deletions
diff --git a/include/serd/env.h b/include/serd/env.h index 906ec2f7..3d4cafd0 100644 --- a/include/serd/env.h +++ b/include/serd/env.h @@ -70,8 +70,8 @@ serd_env_qualify(const SerdEnv* SERD_NULLABLE env, /** Expand `curie`. - Errors: SERD_ERR_BAD_ARG if `curie` is not valid, or SERD_ERR_BAD_CURIE if - prefix is not defined in `env`. + Errors: SERD_BAD_ARG if `curie` is not valid, or SERD_BAD_CURIE if prefix is + not defined in `env`. */ SERD_API SerdStatus serd_env_expand(const SerdEnv* SERD_NULLABLE env, diff --git a/include/serd/status.h b/include/serd/status.h index c640b427..5de96d1d 100644 --- a/include/serd/status.h +++ b/include/serd/status.h @@ -16,17 +16,27 @@ SERD_BEGIN_DECLS /// Return status code typedef enum { - SERD_SUCCESS, ///< No error - SERD_FAILURE, ///< Non-fatal failure - SERD_ERR_UNKNOWN, ///< Unknown error - SERD_ERR_BAD_SYNTAX, ///< Invalid syntax - SERD_ERR_BAD_ARG, ///< Invalid argument - SERD_ERR_NOT_FOUND, ///< Not found - SERD_ERR_ID_CLASH, ///< Encountered clashing blank node IDs - SERD_ERR_BAD_CURIE, ///< Invalid CURIE (e.g. prefix does not exist) - SERD_ERR_INTERNAL, ///< Unexpected internal error (should not happen) - SERD_ERR_BAD_WRITE, ///< Error writing to file/stream - SERD_ERR_BAD_TEXT, ///< Invalid text encoding + SERD_SUCCESS, ///< Success + SERD_FAILURE, ///< Non-fatal failure + SERD_NO_DATA, ///< Missing input + SERD_OVERFLOW, ///< Insufficient space + + SERD_UNKNOWN_ERROR, ///< Unknown error + + SERD_BAD_SYNTAX, ///< Invalid syntax + SERD_BAD_ARG, ///< Invalid argument + SERD_BAD_LABEL, ///< Encountered clashing blank node label + SERD_BAD_CURIE, ///< Invalid CURIE or unknown namespace prefix + SERD_BAD_ALLOC, ///< Memory allocation failed + SERD_BAD_READ, ///< Error reading from file + SERD_BAD_WRITE, ///< Error writing to file + SERD_BAD_STREAM, ///< File or stream error + SERD_BAD_STACK, ///< Stack overflow + SERD_BAD_TEXT, ///< Invalid text encoding + SERD_BAD_CALL, ///< Invalid call + SERD_BAD_URI, ///< Invalid or unresolved URI + SERD_BAD_DATA, ///< Invalid data + SERD_BAD_LITERAL, ///< Invalid literal } SerdStatus; /// Return a string describing a status code diff --git a/src/byte_sink.h b/src/byte_sink.h index 0b4a83b1..0f3d35f5 100644 --- a/src/byte_sink.h +++ b/src/byte_sink.h @@ -41,7 +41,7 @@ serd_byte_sink_flush(SerdByteSink* bsink) const size_t n_out = bsink->sink(bsink->buf, size, bsink->stream); bsink->size = 0; - return (n_out != size) ? SERD_ERR_BAD_WRITE : SERD_SUCCESS; + return (n_out != size) ? SERD_BAD_WRITE : SERD_SUCCESS; } return SERD_SUCCESS; diff --git a/src/byte_source.c b/src/byte_source.c index b56e0102..54b0ad7e 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -19,7 +19,7 @@ serd_byte_source_page(SerdByteSource* const source) if (n_read == 0) { source->file_buf[0] = '\0'; source->eof = true; - return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN + return (source->error_func(source->stream) ? SERD_BAD_STREAM : SERD_FAILURE); } diff --git a/src/byte_source.h b/src/byte_source.h index b6cba5d4..46e1f874 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -93,7 +93,7 @@ serd_byte_source_advance(SerdByteSource* source) if (!source->read_func(&source->read_byte, 1, 1, source->stream)) { source->eof = true; st = - source->error_func(source->stream) ? SERD_ERR_UNKNOWN : SERD_FAILURE; + source->error_func(source->stream) ? SERD_BAD_STREAM : SERD_FAILURE; } } } else if (!source->eof) { @@ -65,7 +65,7 @@ SerdStatus serd_env_set_base_uri(SerdEnv* const env, const SerdNode* const uri) { if (uri && uri->type != SERD_URI) { - return SERD_ERR_BAD_ARG; + return SERD_BAD_ARG; } if (!uri || !uri->buf) { @@ -134,7 +134,7 @@ serd_env_set_prefix(SerdEnv* const env, const SerdNode* const uri) { if (!name->buf || uri->type != SERD_URI) { - return SERD_ERR_BAD_ARG; + return SERD_BAD_ARG; } if (serd_uri_string_has_scheme(uri->buf)) { @@ -196,13 +196,13 @@ serd_env_expand(const SerdEnv* const env, SerdStringView* const uri_suffix) { if (!env) { - return SERD_ERR_BAD_CURIE; + return SERD_BAD_CURIE; } const char* const colon = (const char*)memchr(curie->buf, ':', curie->n_bytes + 1); if (curie->type != SERD_CURIE || !colon) { - return SERD_ERR_BAD_ARG; + return SERD_BAD_ARG; } const size_t name_len = (size_t)(colon - curie->buf); @@ -214,7 +214,7 @@ serd_env_expand(const SerdEnv* const env, uri_suffix->len = curie->n_bytes - name_len - 1; return SERD_SUCCESS; } - return SERD_ERR_BAD_CURIE; + return SERD_BAD_CURIE; } SerdNode @@ -50,7 +50,7 @@ read_HEX(SerdReader* const reader) return (uint8_t)eat_byte_safe(reader, c); } - r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid hexadecimal digit '%c'\n", c); + r_err(reader, SERD_BAD_SYNTAX, "invalid hexadecimal digit '%c'\n", c); return 0; } @@ -68,7 +68,7 @@ read_UCHAR(SerdReader* const reader, const Ref dest, uint32_t* const char_code) length = 4; break; default: - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } skip_byte(reader, b); @@ -78,7 +78,7 @@ read_UCHAR(SerdReader* const reader, const Ref dest, uint32_t* const char_code) uint32_t code = 0U; for (unsigned i = 0U; i < length; ++i) { if (!(buf[i] = read_HEX(reader))) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } code = (code << (i ? 4U : 0U)) | hex_digit_value(buf[i]); @@ -95,10 +95,8 @@ read_UCHAR(SerdReader* const reader, const Ref dest, uint32_t* const char_code) } else if (code < 0x00110000) { size = 4; } else { - r_err(reader, - SERD_ERR_BAD_SYNTAX, - "unicode character 0x%X out of range\n", - code); + r_err( + reader, SERD_BAD_SYNTAX, "unicode character 0x%X out of range\n", code); push_bytes(reader, dest, replacement_char, 3); *char_code = 0xFFFD; return SERD_SUCCESS; @@ -159,7 +157,7 @@ read_ECHAR(SerdReader* const reader, const Ref dest, SerdNodeFlags* const flags) case '\'': return push_byte(reader, dest, eat_byte_safe(reader, c)); default: - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } } @@ -172,8 +170,8 @@ bad_char(SerdReader* const reader, const char* const fmt, const uint8_t c) b = peek_byte(reader); } - r_err(reader, SERD_ERR_BAD_SYNTAX, fmt, c); - return reader->strict ? SERD_ERR_BAD_SYNTAX : SERD_FAILURE; + r_err(reader, SERD_BAD_SYNTAX, fmt, c); + return reader->strict ? SERD_BAD_SYNTAX : SERD_FAILURE; } static SerdStatus @@ -368,7 +366,7 @@ read_STRING_LITERAL_LONG(SerdReader* const reader, st = read_character(reader, ref, flags, (uint8_t)q2); } } else if (c == EOF) { - st = r_err(reader, SERD_ERR_BAD_SYNTAX, "end of file in long string\n"); + st = r_err(reader, SERD_BAD_SYNTAX, "end of file in long string\n"); } else { st = read_character(reader, ref, flags, (uint8_t)eat_byte_safe(reader, c)); @@ -392,11 +390,10 @@ read_STRING_LITERAL(SerdReader* const reader, const int c = peek_byte(reader); switch (c) { case EOF: - return r_err( - reader, SERD_ERR_BAD_SYNTAX, "end of file in short string\n"); + return r_err(reader, SERD_BAD_SYNTAX, "end of file in short string\n"); case '\n': case '\r': - return r_err(reader, SERD_ERR_BAD_SYNTAX, "line end in short string\n"); + return r_err(reader, SERD_BAD_SYNTAX, "line end in short string\n"); case '\\': skip_byte(reader, c); TRY(st, read_string_escape(reader, ref, flags)); @@ -411,7 +408,7 @@ read_STRING_LITERAL(SerdReader* const reader, } } - return eat_byte_check(reader, q) ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; + return eat_byte_check(reader, q) ? SERD_SUCCESS : SERD_BAD_SYNTAX; } static SerdStatus @@ -422,7 +419,7 @@ read_String(SerdReader* const reader, const int q1 = eat_byte_safe(reader, peek_byte(reader)); const int q2 = peek_byte(reader); if (q2 == EOF) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + return r_err(reader, SERD_BAD_SYNTAX, "unexpected end of file\n"); } if (q2 != q1) { // Short string (not triple quoted) @@ -432,7 +429,7 @@ read_String(SerdReader* const reader, skip_byte(reader, q2); const int q3 = peek_byte(reader); if (q3 == EOF) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + return r_err(reader, SERD_BAD_SYNTAX, "unexpected end of file\n"); } if (q3 != q1) { // Empty short string ("" or '') @@ -441,7 +438,7 @@ read_String(SerdReader* const reader, if (!fancy_syntax(reader)) { return r_err( - reader, SERD_ERR_BAD_SYNTAX, "syntax does not support long literals\n"); + reader, SERD_BAD_SYNTAX, "syntax does not support long literals\n"); } skip_byte(reader, q3); @@ -478,10 +475,9 @@ read_PN_CHARS_BASE(SerdReader* const reader, const Ref dest) read_utf8_code(reader, dest, &code, (uint8_t)c); if (!is_PN_CHARS_BASE(code)) { - r_err( - reader, SERD_ERR_BAD_SYNTAX, "invalid character U+%04X in name\n", code); + r_err(reader, SERD_BAD_SYNTAX, "invalid character U+%04X in name\n", code); if (reader->strict) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } } @@ -515,7 +511,7 @@ read_PN_CHARS(SerdReader* const reader, const Ref dest) if (!is_PN_CHARS(code)) { return r_err( - reader, SERD_ERR_BAD_SYNTAX, "invalid character U+%04X in name\n", code); + reader, SERD_BAD_SYNTAX, "invalid character U+%04X in name\n", code); } return st; @@ -532,7 +528,7 @@ read_PERCENT(SerdReader* const reader, const Ref dest) return push_byte(reader, dest, h2); } - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } static SerdStatus @@ -565,7 +561,7 @@ read_PN_LOCAL_ESC(SerdReader* const reader, const Ref dest) push_byte(reader, dest, eat_byte_safe(reader, c)); break; default: - return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid escape\n"); + return r_err(reader, SERD_BAD_SYNTAX, "invalid escape\n"); } return SERD_SUCCESS; @@ -620,7 +616,7 @@ read_PN_LOCAL(SerdReader* const reader, const Ref dest, bool* const ate_dot) if (c == '.' || c == ':') { push_byte(reader, dest, eat_byte_safe(reader, c)); } else if ((st = read_PLX(reader, dest)) > SERD_FAILURE) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad escape\n"); + return r_err(reader, SERD_BAD_SYNTAX, "bad escape\n"); } else if (st != SERD_SUCCESS && (st = read_PN_CHARS(reader, dest))) { break; } @@ -653,7 +649,7 @@ read_PN_PREFIX_tail(SerdReader* const reader, const Ref dest) const SerdNode* const n = deref(reader, dest); if (n->buf[n->n_bytes - 1] == '.' && read_PN_CHARS(reader, dest)) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "prefix ends with '.'\n"); + return r_err(reader, SERD_BAD_SYNTAX, "prefix ends with '.'\n"); } return SERD_SUCCESS; @@ -672,7 +668,7 @@ read_LANGTAG(SerdReader* const reader, Ref* const dest) { int c = peek_byte(reader); if (!is_alpha(c)) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected '%c'\n", c); + return r_err(reader, SERD_BAD_SYNTAX, "unexpected '%c'\n", c); } *dest = push_node(reader, SERD_LITERAL, "", 0); @@ -698,17 +694,17 @@ read_IRIREF_scheme(SerdReader* const reader, const Ref dest) { int c = peek_byte(reader); if (!is_alpha(c)) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad IRI scheme start '%c'\n", c); + return r_err(reader, SERD_BAD_SYNTAX, "bad IRI scheme start '%c'\n", c); } while ((c = peek_byte(reader)) != EOF) { if (c == '>') { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "missing IRI scheme\n"); + return r_err(reader, SERD_BAD_SYNTAX, "missing IRI scheme\n"); } if (!is_uri_scheme_char(c)) { return r_err(reader, - SERD_ERR_BAD_SYNTAX, + SERD_BAD_SYNTAX, "bad IRI scheme char U+%04X (%c)\n", (unsigned)c, (char)c); @@ -720,21 +716,21 @@ read_IRIREF_scheme(SerdReader* const reader, const Ref dest) } } - return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + return r_err(reader, SERD_BAD_SYNTAX, "unexpected end of file\n"); } static SerdStatus read_IRIREF(SerdReader* const reader, Ref* const dest) { if (!eat_byte_check(reader, '<')) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } *dest = push_node(reader, SERD_URI, "", 0); if (!fancy_syntax(reader) && read_IRIREF_scheme(reader, *dest)) { *dest = pop_node(reader, *dest); - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected IRI scheme\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected IRI scheme\n"); } SerdStatus st = SERD_SUCCESS; @@ -745,8 +741,7 @@ read_IRIREF(SerdReader* const reader, Ref* const dest) case '"': case '<': *dest = pop_node(reader, *dest); - return r_err( - reader, SERD_ERR_BAD_SYNTAX, "invalid IRI character '%c'\n", c); + return r_err(reader, SERD_BAD_SYNTAX, "invalid IRI character '%c'\n", c); case '>': return SERD_SUCCESS; @@ -754,7 +749,7 @@ read_IRIREF(SerdReader* const reader, Ref* const dest) case '\\': if (read_UCHAR(reader, *dest, &code)) { *dest = pop_node(reader, *dest); - return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid IRI escape\n"); + return r_err(reader, SERD_BAD_SYNTAX, "invalid IRI escape\n"); } switch (code) { @@ -764,7 +759,7 @@ read_IRIREF(SerdReader* const reader, Ref* const dest) case '>': *dest = pop_node(reader, *dest); return r_err(reader, - SERD_ERR_BAD_SYNTAX, + SERD_BAD_SYNTAX, "invalid escaped IRI character U+%04X\n", code); default: @@ -778,13 +773,12 @@ read_IRIREF(SerdReader* const reader, Ref* const dest) case '|': case '}': *dest = pop_node(reader, *dest); - return r_err( - reader, SERD_ERR_BAD_SYNTAX, "invalid IRI character '%c'\n", c); + return r_err(reader, SERD_BAD_SYNTAX, "invalid IRI character '%c'\n", c); default: if (c <= 0x20) { st = r_err(reader, - SERD_ERR_BAD_SYNTAX, + SERD_BAD_SYNTAX, "invalid IRI character (escape %%%02X)\n", (unsigned)c); if (reader->strict) { @@ -798,7 +792,7 @@ read_IRIREF(SerdReader* const reader, Ref* const dest) } else if (read_utf8_character(reader, *dest, (uint8_t)c)) { if (reader->strict) { *dest = pop_node(reader, *dest); - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } } } @@ -839,7 +833,7 @@ read_0_9(SerdReader* const reader, const Ref str, const bool at_least_one) } if (at_least_one && count == 0) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected digit\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected digit\n"); } return SERD_SUCCESS; @@ -952,7 +946,7 @@ read_literal(SerdReader* const reader, case '^': skip_byte(reader, '^'); if (!eat_byte_check(reader, '^')) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected '^'\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected '^'\n"); } if ((st = read_iri(reader, datatype, ate_dot))) { @@ -993,7 +987,7 @@ read_verb(SerdReader* const reader, Ref* const dest) if (st > SERD_FAILURE || (st = read_PrefixedName(reader, *dest, false, &ate_dot)) || ate_dot) { *dest = pop_node(reader, *dest); - st = st > SERD_FAILURE ? st : SERD_ERR_BAD_SYNTAX; + st = st > SERD_FAILURE ? st : SERD_BAD_SYNTAX; return r_err(reader, st, "bad verb\n"); } @@ -1007,7 +1001,7 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, { skip_byte(reader, '_'); if (!eat_byte_check(reader, ':')) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } const Ref ref = *dest = push_node(reader, @@ -1020,7 +1014,7 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, push_byte(reader, ref, eat_byte_safe(reader, c)); } else if (read_PN_CHARS(reader, ref)) { *dest = pop_node(reader, *dest); - return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid name start\n"); + return r_err(reader, SERD_BAD_SYNTAX, "invalid name start\n"); } while ((c = peek_byte(reader))) { // Middle: (PN_CHARS | '.')* @@ -1047,7 +1041,7 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, } else if (reader->seen_genid && n->buf[reader->bprefix_len] == 'B') { *dest = pop_node(reader, *dest); return r_err(reader, - SERD_ERR_ID_CLASH, + SERD_BAD_LABEL, "found both 'b' and 'B' blank IDs, prefix required\n"); } } @@ -1094,7 +1088,7 @@ read_anon(SerdReader* const reader, TRY_FAILING(st, read_predicateObjectList(reader, ctx, &ate_dot_in_list)); if (ate_dot_in_list) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "'.' inside blank\n"); + return r_err(reader, SERD_BAD_SYNTAX, "'.' inside blank\n"); } read_ws_star(reader); @@ -1107,7 +1101,7 @@ read_anon(SerdReader* const reader, return st > SERD_FAILURE ? st : (eat_byte_check(reader, ']') == ']') ? SERD_SUCCESS - : SERD_ERR_BAD_SYNTAX; + : SERD_BAD_SYNTAX; } /* If emit is true: recurses, calling statement_sink for every statement @@ -1143,13 +1137,13 @@ read_object(SerdReader* const reader, case '_': break; default: - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected: ':', '<', or '_'\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected: ':', '<', or '_'\n"); } } switch (c) { case EOF: case ')': - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected object\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected object\n"); case '[': simple = false; st = read_anon(reader, *ctx, false, &o); @@ -1198,10 +1192,10 @@ read_object(SerdReader* const reader, datatype = push_node(reader, SERD_URI, XSD_BOOLEAN, XSD_BOOLEAN_LEN); st = SERD_SUCCESS; } else if (read_PN_PREFIX_tail(reader, o) > SERD_FAILURE) { - st = SERD_ERR_BAD_SYNTAX; + st = SERD_BAD_SYNTAX; } else { if ((st = read_PrefixedName(reader, o, false, ate_dot))) { - st = st > SERD_FAILURE ? st : SERD_ERR_BAD_SYNTAX; + st = st > SERD_FAILURE ? st : SERD_BAD_SYNTAX; pop_node(reader, o); return r_err(reader, st, "expected prefixed name\n"); } @@ -1237,7 +1231,7 @@ read_objectList(SerdReader* const reader, ReadContext ctx, bool* const ate_dot) TRY(st, read_object(reader, &ctx, true, ate_dot)); if (!fancy_syntax(reader) && peek_delim(reader, ',')) { return r_err( - reader, SERD_ERR_BAD_SYNTAX, "syntax does not support abbreviation\n"); + reader, SERD_BAD_SYNTAX, "syntax does not support abbreviation\n"); } while (!*ate_dot && eat_delim(reader, ',')) { @@ -1266,7 +1260,7 @@ read_predicateObjectList(SerdReader* const reader, read_ws_star(reader); switch (c = peek_byte(reader)) { case EOF: - return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected end of file\n"); + return r_err(reader, SERD_BAD_SYNTAX, "unexpected end of file\n"); case '.': case ']': case '}': @@ -1278,7 +1272,7 @@ read_predicateObjectList(SerdReader* const reader, } while (c == ';'); if (!ate_semi) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "missing ';' or '.'\n"); + return r_err(reader, SERD_BAD_SYNTAX, "missing ';' or '.'\n"); } } @@ -1298,7 +1292,7 @@ end_collection(SerdReader* const reader, *ctx.flags &= ~(unsigned)SERD_LIST_CONT; if (!st) { return (eat_byte_check(reader, ')') == ')') ? SERD_SUCCESS - : SERD_ERR_BAD_SYNTAX; + : SERD_BAD_SYNTAX; } return st; @@ -1389,7 +1383,7 @@ read_subject(SerdReader* const reader, if (ate_dot) { pop_node(reader, *dest); - return r_err(reader, SERD_ERR_BAD_SYNTAX, "subject ends with '.'\n"); + return r_err(reader, SERD_BAD_SYNTAX, "subject ends with '.'\n"); } return st; @@ -1404,7 +1398,7 @@ read_labelOrSubject(SerdReader* const reader, Ref* const dest) skip_byte(reader, '['); read_ws_star(reader); if (!eat_byte_check(reader, ']')) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } *dest = blank_id(reader); return SERD_SUCCESS; @@ -1414,7 +1408,7 @@ read_labelOrSubject(SerdReader* const reader, Ref* const dest) if (!read_iri(reader, dest, &ate_dot)) { return SERD_SUCCESS; } else { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected label or subject\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected label or subject\n"); } } } @@ -1458,11 +1452,11 @@ read_base(SerdReader* const reader, const bool sparql, const bool token) read_ws_star(reader); if (!sparql) { - return eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; + return eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_BAD_SYNTAX; } if (peek_byte(reader) == '.') { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "full stop after SPARQL BASE\n"); + return r_err(reader, SERD_BAD_SYNTAX, "full stop after SPARQL BASE\n"); } return SERD_SUCCESS; @@ -1482,7 +1476,7 @@ read_prefixID(SerdReader* const reader, const bool sparql, const bool token) if (eat_byte_check(reader, ':') != ':') { pop_node(reader, name); - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } read_ws_star(reader); @@ -1498,7 +1492,7 @@ read_prefixID(SerdReader* const reader, const bool sparql, const bool token) pop_node(reader, name); if (!sparql) { read_ws_star(reader); - st = eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX; + st = eat_byte_check(reader, '.') ? SERD_SUCCESS : SERD_BAD_SYNTAX; } return st; @@ -1513,7 +1507,7 @@ read_directive(SerdReader* const reader) switch (peek_byte(reader)) { case 'B': case 'P': - return r_err(reader, SERD_ERR_BAD_SYNTAX, "uppercase directive\n"); + return r_err(reader, SERD_BAD_SYNTAX, "uppercase directive\n"); } } @@ -1528,14 +1522,14 @@ read_directive(SerdReader* const reader) break; } - return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid directive\n"); + return r_err(reader, SERD_BAD_SYNTAX, "invalid directive\n"); } static SerdStatus read_wrappedGraph(SerdReader* const reader, ReadContext* const ctx) { if (!eat_byte_check(reader, '{')) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } read_ws_star(reader); @@ -1545,7 +1539,7 @@ read_wrappedGraph(SerdReader* const reader, ReadContext* const ctx) ctx->subject = 0; SerdStatus st = read_subject(reader, *ctx, &ctx->subject, &s_type); if (st) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "bad subject\n"); + return r_err(reader, SERD_BAD_SYNTAX, "bad subject\n"); } if ((st = read_triples(reader, *ctx, &ate_dot)) && s_type != '[') { @@ -1563,7 +1557,7 @@ read_wrappedGraph(SerdReader* const reader, ReadContext* const ctx) skip_byte(reader, '}'); read_ws_star(reader); if (peek_byte(reader) == '.') { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "graph followed by '.'\n"); + return r_err(reader, SERD_BAD_SYNTAX, "graph followed by '.'\n"); } return SERD_SUCCESS; @@ -1605,7 +1599,7 @@ read_n3_statement(SerdReader* const reader) case '@': if (!fancy_syntax(reader)) { return r_err( - reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + reader, SERD_BAD_SYNTAX, "syntax does not support directives\n"); } TRY(st, read_directive(reader)); read_ws_star(reader); @@ -1615,8 +1609,7 @@ read_n3_statement(SerdReader* const reader) TRY(st, read_wrappedGraph(reader, &ctx)); read_ws_star(reader); } else { - return r_err( - reader, SERD_ERR_BAD_SYNTAX, "syntax does not support graphs\n"); + return r_err(reader, SERD_BAD_SYNTAX, "syntax does not support graphs\n"); } break; default: @@ -1637,10 +1630,10 @@ read_n3_statement(SerdReader* const reader) read_ws_star(reader); } else if (!tokcmp(reader, ctx.subject, "true", 4) || !tokcmp(reader, ctx.subject, "false", 5)) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected subject\n"); + return r_err(reader, SERD_BAD_SYNTAX, "expected subject\n"); } else if (read_ws_star(reader) && peek_byte(reader) == '{') { if (s_type == '(' || (s_type == '[' && !*ctx.flags)) { - return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid graph name\n"); + return r_err(reader, SERD_BAD_SYNTAX, "invalid graph name\n"); } ctx.graph = ctx.subject; ctx.subject = 0; @@ -1653,15 +1646,14 @@ read_n3_statement(SerdReader* const reader) } if (ate_dot) { - return r_err( - reader, SERD_ERR_BAD_SYNTAX, "unexpected end of statement\n"); + return r_err(reader, SERD_BAD_SYNTAX, "unexpected end of statement\n"); } - return st > SERD_FAILURE ? st : SERD_ERR_BAD_SYNTAX; + return st > SERD_FAILURE ? st : SERD_BAD_SYNTAX; } else if (!ate_dot) { read_ws_star(reader); - st = (eat_byte_check(reader, '.') == '.') ? SERD_SUCCESS - : SERD_ERR_BAD_SYNTAX; + st = + (eat_byte_check(reader, '.') == '.') ? SERD_SUCCESS : SERD_BAD_SYNTAX; } ctx.subject = pop_node(reader, ctx.subject); @@ -1720,7 +1712,7 @@ read_nquads_statement(SerdReader* const reader) if (peek_byte(reader) == '@') { return r_err( - reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + reader, SERD_BAD_SYNTAX, "syntax does not support directives\n"); } // subject predicate object @@ -1746,7 +1738,7 @@ read_nquads_statement(SerdReader* const reader) // Terminating '.' read_ws_star(reader); if (!eat_byte_check(reader, '.')) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } } diff --git a/src/reader.c b/src/reader.c index 00f5e73f..6e520208 100644 --- a/src/reader.c +++ b/src/reader.c @@ -272,13 +272,13 @@ serd_reader_read_file(SerdReader* const reader, const char* const uri) { char* const path = serd_file_uri_parse(uri, NULL); if (!path) { - return SERD_ERR_BAD_ARG; + return SERD_BAD_ARG; } FILE* fd = serd_fopen(path, "rb"); if (!fd) { serd_free(path); - return SERD_ERR_UNKNOWN; + return SERD_BAD_STREAM; } SerdStatus ret = serd_reader_read_file_handle(reader, fd, path); @@ -296,8 +296,8 @@ skip_bom(SerdReader* const me) serd_byte_source_advance(&me->source) || serd_byte_source_peek(&me->source) != 0xBF || serd_byte_source_advance(&me->source)) { - r_err(me, SERD_ERR_BAD_SYNTAX, "corrupt byte order mark\n"); - return SERD_ERR_BAD_SYNTAX; + r_err(me, SERD_BAD_SYNTAX, "corrupt byte order mark\n"); + return SERD_BAD_SYNTAX; } } diff --git a/src/reader.h b/src/reader.h index e24bb31a..1dcf926f 100644 --- a/src/reader.h +++ b/src/reader.h @@ -152,7 +152,7 @@ eat_byte_check(SerdReader* reader, const int byte) { const int c = peek_byte(reader); if (c != byte) { - r_err(reader, SERD_ERR_BAD_SYNTAX, "expected '%c', not '%c'\n", byte, c); + r_err(reader, SERD_BAD_SYNTAX, "expected '%c', not '%c'\n", byte, c); return 0; } return eat_byte_safe(reader, byte); @@ -163,7 +163,7 @@ eat_string(SerdReader* reader, const char* str, unsigned n) { for (unsigned i = 0; i < n; ++i) { if (!eat_byte_check(reader, str[i])) { - return SERD_ERR_BAD_SYNTAX; + return SERD_BAD_SYNTAX; } } return SERD_SUCCESS; diff --git a/src/serdi.c b/src/serdi.c index d3ff0813..baa19a36 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -391,7 +391,7 @@ main(int argc, char** argv) if (fclose(out_fd)) { perror("serdi: write error"); - st = SERD_ERR_UNKNOWN; + st = SERD_BAD_STREAM; } return (st > SERD_FAILURE) ? 1 : 0; diff --git a/src/string.c b/src/string.c index 4d8f4f09..5281d500 100644 --- a/src/string.c +++ b/src/string.c @@ -27,24 +27,42 @@ serd_strerror(const SerdStatus status) return "Success"; case SERD_FAILURE: return "Non-fatal failure"; - case SERD_ERR_UNKNOWN: + case SERD_NO_DATA: + return "Unexpected end of input"; + case SERD_OVERFLOW: + return "Insufficient space"; + + case SERD_UNKNOWN_ERROR: break; - case SERD_ERR_BAD_SYNTAX: + + case SERD_BAD_SYNTAX: return "Invalid syntax"; - case SERD_ERR_BAD_ARG: + case SERD_BAD_ARG: return "Invalid argument"; - case SERD_ERR_NOT_FOUND: - return "Not found"; - case SERD_ERR_ID_CLASH: - return "Blank node ID clash"; - case SERD_ERR_BAD_CURIE: - return "Invalid CURIE"; - case SERD_ERR_INTERNAL: - return "Internal error"; - case SERD_ERR_BAD_WRITE: - return "Error writing to file/stream"; - case SERD_ERR_BAD_TEXT: + case SERD_BAD_LABEL: + return "Clashing blank node label"; + case SERD_BAD_CURIE: + return "Invalid CURIE or unknown namespace prefix"; + case SERD_BAD_ALLOC: + return "Memory allocation failed"; + case SERD_BAD_READ: + return "Error reading from file"; + case SERD_BAD_WRITE: + return "Error writing to file"; + case SERD_BAD_STREAM: + return "File or stream error"; + case SERD_BAD_STACK: + return "Stack overflow"; + case SERD_BAD_TEXT: return "Invalid text encoding"; + case SERD_BAD_CALL: + return "Invalid call"; + case SERD_BAD_URI: + return "Invalid or unresolved URI"; + case SERD_BAD_DATA: + return "Invalid data"; + case SERD_BAD_LITERAL: + return "Invalid literal"; } return "Unknown error"; diff --git a/src/writer.c b/src/writer.c index 612c2620..43f2ebe5 100644 --- a/src/writer.c +++ b/src/writer.c @@ -244,9 +244,9 @@ sink(const void* buf, size_t len, SerdWriter* writer) if (written != len) { if (errno) { const char* const message = strerror(errno); - w_err(writer, SERD_ERR_BAD_WRITE, "write error (%s)\n", message); + w_err(writer, SERD_BAD_WRITE, "write error (%s)\n", message); } else { - w_err(writer, SERD_ERR_BAD_WRITE, "write error\n"); + w_err(writer, SERD_BAD_WRITE, "write error\n"); } } @@ -256,7 +256,7 @@ sink(const void* buf, size_t len, SerdWriter* writer) SERD_NODISCARD static inline SerdStatus esink(const void* buf, size_t len, SerdWriter* writer) { - return sink(buf, len, writer) == len ? SERD_SUCCESS : SERD_ERR_BAD_WRITE; + return sink(buf, len, writer) == len ? SERD_SUCCESS : SERD_BAD_WRITE; } // Write a single character, as an escape for single byte characters @@ -271,8 +271,7 @@ write_character(SerdWriter* writer, const uint32_t c = parse_utf8_char(utf8, size); switch (*size) { case 0: - *st = - w_err(writer, SERD_ERR_BAD_TEXT, "invalid UTF-8 start: %X\n", utf8[0]); + *st = w_err(writer, SERD_BAD_TEXT, "invalid UTF-8 start: %X\n", utf8[0]); return 0; case 1: snprintf(escape, sizeof(escape), "\\u%04X", utf8[0]); @@ -331,7 +330,7 @@ write_uri(SerdWriter* writer, const char* utf8, size_t n_bytes, SerdStatus* st) const size_t n_bulk = sink(&utf8[i], j - i, writer); len += n_bulk; if (n_bulk != j - i) { - *st = SERD_ERR_BAD_WRITE; + *st = SERD_BAD_WRITE; return len; } @@ -366,7 +365,7 @@ ewrite_uri(SerdWriter* writer, const char* utf8, size_t n_bytes) SerdStatus st = SERD_SUCCESS; write_uri(writer, utf8, n_bytes, &st); - return (st == SERD_ERR_BAD_WRITE || (writer->flags & SERD_WRITE_STRICT)) + return (st == SERD_BAD_WRITE || (writer->flags & SERD_WRITE_STRICT)) ? st : SERD_SUCCESS; } @@ -754,7 +753,7 @@ write_uri_node(SerdWriter* const writer, if (!has_scheme && !supports_uriref(writer) && !serd_env_base_uri(writer->env, NULL)->buf) { return w_err(writer, - SERD_ERR_BAD_ARG, + SERD_BAD_ARG, "syntax does not support URI reference <%s>\n", node->buf); } @@ -961,7 +960,7 @@ serd_writer_write_statement(SerdWriter* writer, if (!is_resource(subject) || !is_resource(predicate) || !object || !object->buf) { - return SERD_ERR_BAD_ARG; + return SERD_BAD_ARG; } // Simple case: write a line of NTriples or NQuads @@ -1101,8 +1100,7 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) } if (serd_stack_is_empty(&writer->anon_stack)) { - return w_err( - writer, SERD_ERR_UNKNOWN, "unexpected end of anonymous node\n"); + return w_err(writer, SERD_BAD_CALL, "unexpected end of anonymous node\n"); } // Write the end separator ']' and pop the context diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 0fcc5ba3..f6dfe512 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -435,7 +435,7 @@ test_write_errors(void) serd_writer_set_error_sink(writer, quiet_error_sink, NULL); const SerdStatus st = serd_reader_read_string(reader, doc_string); - assert(st == SERD_ERR_BAD_WRITE); + assert(st == SERD_BAD_WRITE); serd_reader_free(reader); serd_writer_free(writer); diff --git a/test/test_string.c b/test/test_string.c index 3e6e77e5..49106f0a 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -29,7 +29,7 @@ test_strerror(void) { const char* msg = serd_strerror(SERD_SUCCESS); assert(!strcmp(msg, "Success")); - for (int i = SERD_FAILURE; i <= SERD_ERR_BAD_TEXT; ++i) { + for (int i = SERD_FAILURE; i <= SERD_BAD_LITERAL; ++i) { msg = serd_strerror((SerdStatus)i); assert(strcmp(msg, "Success")); } diff --git a/test/test_writer.c b/test/test_writer.c index 0a823d2b..6a7a35b5 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -118,10 +118,10 @@ test_strict_write(void) SerdNode bad_uri = serd_node_from_string(SERD_URI, (const char*)bad_str); assert(serd_writer_write_statement( - writer, 0, NULL, &s, &p, &bad_lit, NULL, NULL) == SERD_ERR_BAD_TEXT); + writer, 0, NULL, &s, &p, &bad_lit, NULL, NULL) == SERD_BAD_TEXT); assert(serd_writer_write_statement( - writer, 0, NULL, &s, &p, &bad_uri, NULL, NULL) == SERD_ERR_BAD_TEXT); + writer, 0, NULL, &s, &p, &bad_uri, NULL, NULL) == SERD_BAD_TEXT); serd_writer_free(writer); serd_env_free(env); @@ -152,7 +152,7 @@ test_write_error(void) SERD_TURTLE, (SerdWriterFlags)0, env, NULL, error_sink, NULL); assert(writer); st = serd_writer_write_statement(writer, 0U, NULL, &u, &u, &u, NULL, NULL); - assert(st == SERD_ERR_BAD_WRITE); + assert(st == SERD_BAD_WRITE); serd_writer_free(writer); serd_env_free(env); |