-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch_exceptions.cpp
More file actions
70 lines (58 loc) · 2.36 KB
/
ch_exceptions.cpp
File metadata and controls
70 lines (58 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
+----------------------------------------------------------------------+
| php-clickhouse — native async ClickHouse client for PHP TrueAsync |
+----------------------------------------------------------------------+
| Licensed under the Apache License, Version 2.0 (the "License"). |
+----------------------------------------------------------------------+
C++ -> PHP exception translation. Maps clickhouse-cpp's exception hierarchy
(and our transport's ConnectionError) onto the registered PHP classes.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "Zend/zend_exceptions.h"
}
#include <memory>
#include <string>
#include <clickhouse/exceptions.h>
#include "ch_exceptions.h"
zend_class_entry *ce_ch_exception = nullptr;
zend_class_entry *ce_ch_connection_exception = nullptr;
zend_class_entry *ce_ch_server_exception = nullptr;
zend_class_entry *ce_ch_protocol_exception = nullptr;
void ch_translate_and_throw(const std::exception &e)
{
/* A PHP exception (e.g. coroutine cancellation) is already pending — let
* it win rather than overwrite it. */
if (EG(exception) != nullptr) {
return;
}
/* Server-side error carries a ClickHouse error code. */
if (const auto *se = dynamic_cast<const clickhouse::ServerException *>(&e)) {
zend_throw_exception(ce_ch_server_exception, se->what(), se->GetCode());
return;
}
/* Decode / checksum / compression failures. */
if (dynamic_cast<const clickhouse::ProtocolError *>(&e) != nullptr
|| dynamic_cast<const clickhouse::CompressionError *>(&e) != nullptr
|| dynamic_cast<const clickhouse::AssertionError *>(&e) != nullptr) {
zend_throw_exception(ce_ch_protocol_exception, e.what(), 0);
return;
}
/* Transport-level network / IO failure. */
if (dynamic_cast<const chasync::ConnectionError *>(&e) != nullptr) {
zend_throw_exception(ce_ch_connection_exception, e.what(), 0);
return;
}
/* Caller-side mistakes (bad arguments / data shape) are a LogicException,
* not a runtime ClickHouseException — surface PHP's standard ValueError. */
if (dynamic_cast<const clickhouse::ValidationError *>(&e) != nullptr) {
zend_throw_exception(zend_ce_value_error, e.what(), 0);
return;
}
/* clickhouse::Error base (UnimplementedError, OpenSSLError) and any other
* std::exception fall back to the base. */
zend_throw_exception(ce_ch_exception, e.what(), 0);
}