Skip to content

Commit c6de1ce

Browse files
committed
#26 - Done. The error states of the transcoder are separated to Tarantool error and Transcoder error
1 parent 020748c commit c6de1ce

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

src/debug.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ static inline void dd(const char* fmt, ...) { }
6262

6363
#include <stdarg.h>
6464

65-
static inline void dd(const char* fmt, ...) { }
65+
static inline void
66+
dd(const char* fmt, ...) { }
6667

6768
# endif
6869

6970
#endif /* MY_DEBUG */
7071

72+
static inline const u_char*
73+
get_str_safe(const u_char *str) {
74+
return (str ? str : (const u_char *)"NULL");
75+
}
76+
7177
#if (NGX_HAVE_VARIADIC_MACROS)
7278
# define log_crit(log, ...) \
7379
ngx_log_error_core(NGX_LOG_CRIT, (log), 0, __VA_ARGS__)
@@ -76,7 +82,8 @@ static inline void dd(const char* fmt, ...) { }
7682
/** TODO
7783
* Warn. user here
7884
*/
79-
static inline void crit(...) {}
85+
static inline void
86+
crit(...) {}
8087
#endif /* NGX_HAVE_VARIADIC_MACROS */
8188

8289
#endif

src/ngx_http_tnt_handlers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,14 @@ ngx_http_tnt_query_handler(ngx_http_request_t *r)
813813
(size_t)ctx->preset_method_len, 1))
814814
{
815815
err = get_error_text(HTTP_REQUEST_TOO_LARGE);
816-
crit("ngx_http_tnt_query_handler - %s", err->msg.data);
816+
crit("ngx_http_tnt_query_handler - %s", get_str_safe(err->msg.data));
817817
return NGX_ERROR;
818818
}
819819

820820
rc = ngx_http_tnt_get_request_data(r, tlcf, &tp);
821821
if (rc != NGX_OK) {
822822
err = get_error_text(HTTP_REQUEST_TOO_LARGE);
823-
crit("ngx_http_tnt_query_handler - %s", err->msg.data);
823+
crit("ngx_http_tnt_query_handler - %s", get_str_safe(err->msg.data));
824824
return rc;
825825
}
826826

src/ngx_http_tnt_module.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -599,43 +599,44 @@ ngx_http_tnt_send_reply(ngx_http_request_t *r,
599599

600600
rc = tp_transcode(&tc, (char *)ctx->tp_cache->start,
601601
ctx->tp_cache->end - ctx->tp_cache->start);
602-
if (rc == TP_TRANSCODE_OK) {
602+
if (rc != TP_TRANSCODE_ERROR) {
603603

604+
/* Finishing
605+
*/
604606
size_t complete_msg_size = 0;
605607
rc = tp_transcode_complete(&tc, &complete_msg_size);
606608
if (rc == TP_TRANSCODE_ERROR) {
609+
crit("[BUG] failed to complete output transcoding. UNKNOWN ERROR");
610+
goto error_exit;
611+
}
607612

608-
crit("[BUG] failed to complete output transcoding");
609-
613+
/* Ok. We send an error to the client from the Tarantool
614+
*/
615+
if (rc == TP_TNT_ERROR) {
610616
ngx_pfree(r->pool, output);
611617

612-
const ngx_http_tnt_error_t *e = get_error_text(UNKNOWN_PARSE_ERROR);
613-
output = ngx_http_tnt_set_err(r, e->code, e->msg.data, e->msg.len);
618+
/* Swap output */
619+
output = ngx_http_tnt_set_err(r, tc.errcode,
620+
(u_char *)tc.errmsg,
621+
ngx_strlen(tc.errmsg));
614622
if (output == NULL) {
615623
goto error_exit;
616624
}
617-
618-
goto done;
619-
}
620-
621-
output->last = output->pos + complete_msg_size;
622-
623-
} else if (rc == TP_TRANSCODE_ERROR) {
624-
625-
crit("[BUG] failed to transcode output, err: '%s'", tc.errmsg);
626-
627-
ngx_pfree(r->pool, output);
628-
629-
output = ngx_http_tnt_set_err(r,
630-
tc.errcode,
631-
(u_char *)tc.errmsg,
632-
ngx_strlen(tc.errmsg));
633-
if (output == NULL) {
634-
goto error_exit;
625+
} else {
626+
output->last = output->pos + complete_msg_size;
635627
}
636628
}
629+
/* Transcoder down
630+
*/
631+
else {
632+
crit("[BUG] failed to transcode output. errcode: '%d', errmsg: '%s'",
633+
tc.errcode,
634+
get_str_safe((const u_char *)tc.errmsg));
635+
goto error_exit;
636+
}
637637

638-
done:
638+
/* Transcoding - OK
639+
*/
639640
tp_transcode_free(&tc);
640641

641642
if (ctx->batch_size > 0) {
@@ -655,6 +656,9 @@ ngx_http_tnt_send_reply(ngx_http_request_t *r,
655656
return ngx_http_tnt_output(r, u, output);
656657

657658
error_exit:
659+
if (output) {
660+
ngx_pfree(r->pool, output);
661+
}
658662
tp_transcode_free(&tc);
659663
return NGX_ERROR;
660664
}

src/tp_transcode.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,8 @@ tp2json_transcode_internal(tp2json_t *ctx, const char **beg, const char *end)
932932
mp_decode_uint(beg));
933933
break;
934934
case MP_INT:
935+
/* Well. I guess that's okay. Are you agree?
936+
*/
935937
if (unlikely(len < sizeof("18446744073709551615") - 1))
936938
say_overflow_r(ctx);
937939

@@ -1082,9 +1084,8 @@ tp2json_transcode_internal(tp2json_t *ctx, const char **beg, const char *end)
10821084
static enum tt_result
10831085
tp_reply2json_transcode(void *ctx_, const char *in, size_t in_size)
10841086
{
1085-
enum tt_result rc;
1086-
10871087
tp2json_t *ctx = ctx_;
1088+
enum tt_result rc = TP_TRANSCODE_OK;
10881089

10891090
if (ctx->first_entry) {
10901091

@@ -1106,6 +1107,8 @@ tp_reply2json_transcode(void *ctx_, const char *in, size_t in_size)
11061107
elen, ctx->r.error,
11071108
code_conv(ctx->r.code));
11081109

1110+
rc = TP_TNT_ERROR;
1111+
11091112
} else {
11101113

11111114
if (!ctx->pure_result) {
@@ -1129,11 +1132,11 @@ tp_reply2json_transcode(void *ctx_, const char *in, size_t in_size)
11291132
++ctx->pos;
11301133
}
11311134

1132-
return TP_TRANSCODE_OK;
1135+
return rc;
11331136

11341137
error_exit:
11351138
ctx->pos = ctx->output;
1136-
return TP_TRANSCODE_ERROR;
1139+
return rc;
11371140
}
11381141

11391142
static enum tt_result
@@ -1184,7 +1187,7 @@ tp2json_complete(void *ctx_, size_t *complete_msg_size)
11841187
}
11851188

11861189
/**
1187-
* Known codecs
1190+
* List of codecs
11881191
*/
11891192
#define CODEC(create_, transcode_, complete_, free_) \
11901193
(tp_codec_t) { \

src/tp_transcode.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ typedef struct tp_transcode_init_args {
136136
*/
137137
enum tt_result {
138138
TP_TRANSCODE_OK = 1,
139-
TP_TRANSCODE_ERROR
139+
TP_TRANSCODE_ERROR,
140+
TP_TNT_ERROR,
140141
};
141142

142143
ssize_t
@@ -160,6 +161,7 @@ void tp_transcode_free(tp_transcode_t *t);
160161
*
161162
* Returns TP_TRANSCODE_OK if bytes enought for finish transcoding
162163
* Returns TP_TRANSCODE_ERROR if error occurred
164+
* Returns TP_AT_ERROR if error occurred
163165
*/
164166
enum tt_result tp_transcode(tp_transcode_t *t, const char *b, size_t s);
165167

@@ -184,9 +186,9 @@ tp_reply_to_json_set_options(tp_transcode_t *t,
184186
int multireturn_skip_count);
185187

186188
/**
187-
* WARNING! tp_dump() must be use only for debug pupose
189+
* WARNING! tp_dump() - is for debug
188190
*
189-
* Dump Tarantool message in JSON format
191+
* Dump Tarantool message in JSON
190192
* Returns true, false
191193
*/
192194
bool

0 commit comments

Comments
 (0)