@@ -35,12 +35,19 @@ class NodeMidiOutput : public Nan::ObjectWrap
3535
3636 NodeMidiOutput ()
3737 {
38- out = new RtMidiOut ();
38+ try {
39+ out = new RtMidiOut ();
40+ }
41+ catch (RtMidiError &e) {
42+ out = NULL ;
43+ }
3944 }
4045
4146 ~NodeMidiOutput ()
4247 {
43- delete out;
48+ if (out) {
49+ delete out;
50+ }
4451 }
4552
4653 static NAN_METHOD (New)
@@ -61,7 +68,7 @@ class NodeMidiOutput : public Nan::ObjectWrap
6168 {
6269 Nan::HandleScope scope;
6370 NodeMidiOutput* output = Nan::ObjectWrap::Unwrap<NodeMidiOutput>(info.This ());
64- v8::Local<v8::Integer> result = Nan::New<v8::Uint32>(output->out -> getPortCount ());
71+ v8::Local<v8::Integer> result = Nan::New<v8::Uint32>(output-> out ? output-> out -> getPortCount () : 0 );
6572 info.GetReturnValue ().Set (result);
6673 }
6774
@@ -74,14 +81,23 @@ class NodeMidiOutput : public Nan::ObjectWrap
7481 }
7582
7683 unsigned int portNumber = info[0 ]->Uint32Value ();
77- v8::Local<v8::String> result = Nan::New<v8::String>(output->out ->getPortName (portNumber).c_str ()).ToLocalChecked ();
78- info.GetReturnValue ().Set (result);
84+
85+ try {
86+ v8::Local<v8::String> result = Nan::New<v8::String>(output->out ? output->out ->getPortName (portNumber).c_str () : " " ).ToLocalChecked ();
87+ info.GetReturnValue ().Set (result);
88+ }
89+ catch (RtMidiError& e) {
90+ info.GetReturnValue ().Set (Nan::New<v8::String>(" " ).ToLocalChecked ());
91+ }
7992 }
8093
8194 static NAN_METHOD (OpenPort)
8295 {
8396 Nan::HandleScope scope;
8497 NodeMidiOutput* output = Nan::ObjectWrap::Unwrap<NodeMidiOutput>(info.This ());
98+
99+ if (!output->out ) return ;
100+
85101 if (info.Length () == 0 || !info[0 ]->IsUint32 ()) {
86102 return Nan::ThrowTypeError (" First argument must be an integer" );
87103 }
@@ -90,28 +106,44 @@ class NodeMidiOutput : public Nan::ObjectWrap
90106 return Nan::ThrowRangeError (" Invalid MIDI port number" );
91107 }
92108
93- output->out ->openPort (portNumber);
109+ try {
110+ output->out ->openPort (portNumber);
111+ }
112+ catch (RtMidiError& e) {
113+ ;
114+ }
94115 return ;
95116 }
96117
97118 static NAN_METHOD (OpenVirtualPort)
98119 {
99120 Nan::HandleScope scope;
100121 NodeMidiOutput* output = Nan::ObjectWrap::Unwrap<NodeMidiOutput>(info.This ());
122+
123+ if (!output->out ) return ;
124+
101125 if (info.Length () == 0 || !info[0 ]->IsString ()) {
102126 return Nan::ThrowTypeError (" First argument must be a string" );
103127 }
104128
105129 std::string name (*v8::String::Utf8Value (info[0 ].As <v8::String>()));
106130
107- output->out ->openVirtualPort (name);
131+ try {
132+ output->out ->openVirtualPort (name);
133+ }
134+ catch (RtMidiError& e) {
135+ ;
136+ }
108137 return ;
109138 }
110139
111140 static NAN_METHOD (ClosePort)
112141 {
113142 Nan::HandleScope scope;
114143 NodeMidiOutput* output = Nan::ObjectWrap::Unwrap<NodeMidiOutput>(info.This ());
144+
145+ if (!output->out ) return ;
146+
115147 output->out ->closePort ();
116148 return ;
117149 }
@@ -120,6 +152,9 @@ class NodeMidiOutput : public Nan::ObjectWrap
120152 {
121153 Nan::HandleScope scope;
122154 NodeMidiOutput* output = Nan::ObjectWrap::Unwrap<NodeMidiOutput>(info.This ());
155+
156+ if (!output->out ) return ;
157+
123158 if (info.Length () == 0 || !info[0 ]->IsArray ()) {
124159 return Nan::ThrowTypeError (" First argument must be an array" );
125160 }
@@ -130,7 +165,12 @@ class NodeMidiOutput : public Nan::ObjectWrap
130165 for (int32_t i = 0 ; i != messageLength; ++i) {
131166 messageOutput.push_back (message->Get (Nan::New<v8::Integer>(i))->Int32Value ());
132167 }
133- output->out ->sendMessage (&messageOutput);
168+ try {
169+ output->out ->sendMessage (&messageOutput);
170+ }
171+ catch (RtMidiError& e) {
172+ ;
173+ }
134174 return ;
135175 }
136176};
@@ -179,14 +219,21 @@ class NodeMidiInput : public Nan::ObjectWrap
179219
180220 NodeMidiInput ()
181221 {
182- in = new RtMidiIn ();
222+ try {
223+ in = new RtMidiIn ();
224+ }
225+ catch (RtMidiError& e) {
226+ in = NULL ;
227+ }
183228 uv_mutex_init (&message_mutex);
184229 }
185230
186231 ~NodeMidiInput ()
187232 {
188- in->closePort ();
189- delete in;
233+ if (in) {
234+ in->closePort ();
235+ delete in;
236+ }
190237 uv_mutex_destroy (&message_mutex);
191238 }
192239
@@ -247,7 +294,7 @@ class NodeMidiInput : public Nan::ObjectWrap
247294 {
248295 Nan::HandleScope scope;
249296 NodeMidiInput* input = Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ());
250- v8::Local<v8::Integer> result = Nan::New<v8::Uint32>(input->in -> getPortCount ());
297+ v8::Local<v8::Integer> result = Nan::New<v8::Uint32>(input->in ? input-> in -> getPortCount () : 0 );
251298 info.GetReturnValue ().Set (result);
252299 }
253300
@@ -260,14 +307,22 @@ class NodeMidiInput : public Nan::ObjectWrap
260307 }
261308
262309 unsigned int portNumber = info[0 ]->Uint32Value ();
263- v8::Local<v8::String> result = Nan::New<v8::String>(input->in ->getPortName (portNumber).c_str ()).ToLocalChecked ();
264- info.GetReturnValue ().Set (result);
310+ try {
311+ v8::Local<v8::String> result = Nan::New<v8::String>(input->in ->getPortName (portNumber).c_str ()).ToLocalChecked ();
312+ info.GetReturnValue ().Set (result);
313+ }
314+ catch (RtMidiError& e) {
315+ info.GetReturnValue ().Set (Nan::New<v8::String>(" " ).ToLocalChecked ());
316+ }
265317 }
266318
267319 static NAN_METHOD (OpenPort)
268320 {
269321 Nan::HandleScope scope;
270322 NodeMidiInput* input = Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ());
323+
324+ if (!input->in ) return ;
325+
271326 if (info.Length () == 0 || !info[0 ]->IsUint32 ()) {
272327 return Nan::ThrowTypeError (" First argument must be an integer" );
273328 }
@@ -277,36 +332,57 @@ class NodeMidiInput : public Nan::ObjectWrap
277332 }
278333
279334 input->Ref ();
280- input->in ->setCallback (&NodeMidiInput::Callback, Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ()));
281- input->in ->openPort (portNumber);
335+ try {
336+ input->in ->setCallback (&NodeMidiInput::Callback, Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ()));
337+ input->in ->openPort (portNumber);
338+ }
339+ catch (RtMidiError& e) {
340+ ;
341+ }
282342 return ;
283343 }
284344
285345 static NAN_METHOD (OpenVirtualPort)
286346 {
287347 Nan::HandleScope scope;
288348 NodeMidiInput* input = Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ());
349+
350+ if (!input->in ) return ;
351+
289352 if (info.Length () == 0 || !info[0 ]->IsString ()) {
290353 return Nan::ThrowTypeError (" First argument must be a string" );
291354 }
292355
293356 std::string name (*v8::String::Utf8Value (info[0 ].As <v8::String>()));
294357
295358 input->Ref ();
296- input->in ->setCallback (&NodeMidiInput::Callback, Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ()));
297- input->in ->openVirtualPort (name);
359+ try {
360+ input->in ->setCallback (&NodeMidiInput::Callback, Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ()));
361+ input->in ->openVirtualPort (name);
362+ }
363+ catch (RtMidiError& e) {
364+ ;
365+ }
298366 return ;
299367 }
300368
301369 static NAN_METHOD (ClosePort)
302370 {
303371 Nan::HandleScope scope;
304372 NodeMidiInput* input = Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ());
373+
374+ if (!input->in ) return ;
375+
305376 if (input->in ->isPortOpen ()) {
306377 input->Unref ();
307378 }
308- input->in ->cancelCallback ();
309- input->in ->closePort ();
379+ try {
380+ input->in ->cancelCallback ();
381+ input->in ->closePort ();
382+ }
383+ catch (RtMidiError& e) {
384+ ;
385+ }
310386 uv_close ((uv_handle_t *)&input->message_async , NULL );
311387 return ;
312388 }
@@ -315,6 +391,9 @@ class NodeMidiInput : public Nan::ObjectWrap
315391 {
316392 Nan::HandleScope scope;
317393 NodeMidiInput* input = Nan::ObjectWrap::Unwrap<NodeMidiInput>(info.This ());
394+
395+ if (!input->in ) return ;
396+
318397 if (info.Length () != 3 || !info[0 ]->IsBoolean () || !info[1 ]->IsBoolean () || !info[2 ]->IsBoolean ()) {
319398 return Nan::ThrowTypeError (" Arguments must be boolean" );
320399 }
0 commit comments