1 /*
2  Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation.  The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License, version 2.0, for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 /* NOTE
26 
27   JavaScript accessor methods are used to get properties from an NdbError;
28   the NdbError must remain valid while accessed from JavaScript.
29 
30   After an NdbTransaction is closed, any reference to an NdbError from the
31   NdbTransaction or its NdbOperations becomes invalid.
32 
33 */
34 
35 #include <NdbApi.hpp>
36 
37 #include "adapter_global.h"
38 #include "js_wrapper_macros.h"
39 #include "Record.h"
40 #include "NativeMethodCall.h"
41 #include "NdbWrapperErrors.h"
42 
43 using namespace v8;
44 
45 void get_status(Local<String>, const AccessorInfo &);
46 void get_classification(Local<String>, const AccessorInfo &);
47 void get_code(Local<String>, const AccessorInfo &);
48 void get_mysql_code(Local<String>, const AccessorInfo &);
49 void get_message(Local<String>, const AccessorInfo &);
50 
51 
52 class NdbErrorEnvelopeClass : public Envelope {
53 public:
NdbErrorEnvelopeClass()54   NdbErrorEnvelopeClass() : Envelope("NdbError") {
55     addAccessor("status", get_status);
56     addAccessor("classification", get_classification);
57     addAccessor("code", get_code);
58     addAccessor("handler_error_code", get_mysql_code);
59     addAccessor("message", get_message);
60   }
61 };
62 
63 NdbErrorEnvelopeClass NdbErrorEnvelope;
64 
65 // TODO: Verify that all callers have a HandleScope
NdbError_Wrapper(const NdbError & err)66 Local<Value> NdbError_Wrapper(const NdbError &err) {
67   return NdbErrorEnvelope.wrap(& err);
68 }
69 
70 #define V8STRING(MSG) scope.Escape(String::NewFromUtf8(info.GetIsolate(), MSG))
71 
72 #define V8INTEGER(V) scope.Escape(Integer::New(info.GetIsolate(), V))
73 
74 #define MAP_CODE(CODE) \
75   case NdbError::CODE: \
76     info.GetReturnValue().Set(V8STRING(#CODE)); \
77     return;
78 
79 
get_status(Local<String> property,const AccessorInfo & info)80 void get_status(Local<String> property, const AccessorInfo &info) {
81   EscapableHandleScope scope(info.GetIsolate());
82   const NdbError *err = unwrapPointer<const NdbError *>(info.Holder());
83 
84   switch(err->status) {
85     MAP_CODE(Success);
86     MAP_CODE(TemporaryError);
87     MAP_CODE(PermanentError);
88     MAP_CODE(UnknownResult);
89   }
90   info.GetReturnValue().Set(V8STRING("-unknown-"));
91 }
92 
93 
get_classification(Local<String> property,const AccessorInfo & info)94 void get_classification(Local<String> property, const AccessorInfo &info) {
95   EscapableHandleScope scope(info.GetIsolate());
96   const NdbError *err = unwrapPointer<const NdbError *>(info.Holder());
97 
98   switch(err->classification) {
99     MAP_CODE(NoError);
100     MAP_CODE(ApplicationError);
101     MAP_CODE(NoDataFound);
102     MAP_CODE(ConstraintViolation);
103     MAP_CODE(SchemaError);
104     MAP_CODE(UserDefinedError);
105     MAP_CODE(InsufficientSpace);
106     MAP_CODE(TemporaryResourceError);
107     MAP_CODE(NodeRecoveryError);
108     MAP_CODE(OverloadError);
109     MAP_CODE(TimeoutExpired);
110     MAP_CODE(UnknownResultError);
111     MAP_CODE(InternalError);
112     MAP_CODE(FunctionNotImplemented);
113     MAP_CODE(UnknownErrorCode);
114     MAP_CODE(NodeShutdown);
115     MAP_CODE(SchemaObjectExists);
116     MAP_CODE(InternalTemporary);
117   }
118   info.GetReturnValue().Set(V8STRING("-unknown-"));
119 }
120 
get_code(Local<String> property,const AccessorInfo & info)121 void get_code(Local<String> property, const AccessorInfo &info) {
122   EscapableHandleScope scope(info.GetIsolate());
123   const NdbError *err = unwrapPointer<const NdbError *>(info.Holder());
124 
125   info.GetReturnValue().Set(V8INTEGER(err->code));
126 }
127 
get_mysql_code(Local<String> property,const AccessorInfo & info)128 void get_mysql_code(Local<String> property, const AccessorInfo &info) {
129   EscapableHandleScope scope(info.GetIsolate());
130   const NdbError *err = unwrapPointer<const NdbError *>(info.Holder());
131 
132   info.GetReturnValue().Set(V8INTEGER(err->mysql_code));
133 }
134 
get_message(Local<String> property,const AccessorInfo & info)135 void get_message(Local<String> property, const AccessorInfo &info) {
136   EscapableHandleScope scope(info.GetIsolate());
137   const NdbError *err = unwrapPointer<const NdbError *>(info.Holder());
138 
139   info.GetReturnValue().Set(V8STRING(err->message));
140 }
141 
142