[Groonga-commit] groonga/groonga at 87810d9 [master] ruby_load: support load error output

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Oct 8 22:44:20 JST 2013


Kouhei Sutou	2013-10-08 22:44:20 +0900 (Tue, 08 Oct 2013)

  New Revision: 87810d990ec22755ca0fa00c2762ff0cb3da8867
  https://github.com/groonga/groonga/commit/87810d990ec22755ca0fa00c2762ff0cb3da8867

  Message:
    ruby_load: support load error output
    
    TODO: It should be shared with ruby_eval.

  Added files:
    test/command/suite/ruby/load/nonexistent.expected
    test/command/suite/ruby/load/nonexistent.test
  Modified files:
    lib/mrb.c
    plugins/ruby/load.c

  Modified: lib/mrb.c (+7 -0)
===================================================================
--- lib/mrb.c    2013-10-08 22:26:50 +0900 (3d4f9f8)
+++ lib/mrb.c    2013-10-08 22:44:20 +0900 (8024e83)
@@ -25,6 +25,8 @@
 # include <mruby/string.h>
 #endif
 
+#define BUFFER_SIZE 2048
+
 #ifdef GRN_WITH_MRUBY
 #ifdef WIN32
 static char *win32_ruby_scripts_dir = NULL;
@@ -109,6 +111,11 @@ grn_mrb_load(grn_ctx *ctx, const char *path)
     return mrb_nil_value();
   }
   if (!(fp = grn_mrb_open_script(ctx, path))) {
+    mrb_value exception;
+    char message[BUFFER_SIZE];
+    snprintf(message, BUFFER_SIZE - 1, "can't find script: <%s>", path);
+    exception = mrb_exc_new(mrb, E_ARGUMENT_ERROR, message, strlen(message));
+    mrb->exc = mrb_obj_ptr(exception);
     return mrb_nil_value();
   }
 

  Modified: plugins/ruby/load.c (+22 -1)
===================================================================
--- plugins/ruby/load.c    2013-10-08 22:26:50 +0900 (71d43c2)
+++ plugins/ruby/load.c    2013-10-08 22:44:20 +0900 (f80d211)
@@ -19,18 +19,38 @@
 #include <mrb.h>
 #include <output.h>
 #include <db.h>
+#include <ctx_impl.h>
 #include <util.h>
 
 #include <groonga/plugin.h>
 
+#include <mruby.h>
+
 #define VAR GRN_PROC_GET_VAR_BY_OFFSET
 
 static void
 output_result(grn_ctx *ctx, mrb_value result)
 {
-  grn_obj grn_result;
+  mrb_state *mrb = ctx->impl->mrb.state;
 
   GRN_OUTPUT_MAP_OPEN("result", 1);
+  if (mrb->exc) {
+    mrb_value mrb_message;
+    grn_obj grn_message;
+    GRN_OUTPUT_CSTR("exception");
+    GRN_OUTPUT_MAP_OPEN("exception", 1);
+    GRN_OUTPUT_CSTR("message");
+    mrb_message = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "message", 0);
+    GRN_VOID_INIT(&grn_message);
+    if (grn_mrb_to_grn(ctx, mrb_message, &grn_message) == GRN_SUCCESS) {
+      GRN_OUTPUT_OBJ(&grn_message, NULL);
+    } else {
+      GRN_OUTPUT_CSTR("unsupported message type");
+    }
+    grn_obj_unlink(ctx, &grn_message);
+    GRN_OUTPUT_MAP_CLOSE();
+  } else {
+  grn_obj grn_result;
   GRN_OUTPUT_CSTR("value");
   GRN_VOID_INIT(&grn_result);
   if (grn_mrb_to_grn(ctx, result, &grn_result) == GRN_SUCCESS) {
@@ -39,6 +59,7 @@ output_result(grn_ctx *ctx, mrb_value result)
     GRN_OUTPUT_CSTR("unsupported return value");
   }
   grn_obj_unlink(ctx, &grn_result);
+  }
   GRN_OUTPUT_MAP_CLOSE();
 }
 

  Added: test/command/suite/ruby/load/nonexistent.expected (+4 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/ruby/load/nonexistent.expected    2013-10-08 22:44:20 +0900 (a9a4708)
@@ -0,0 +1,4 @@
+register ruby/load
+[[0,0.0,0.0],true]
+ruby_load "nonexistent.rb"
+[[0,0.0,0.0],{"exception":{"message":"can't find script: <nonexistent.rb>"}}]

  Added: test/command/suite/ruby/load/nonexistent.test (+5 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/ruby/load/nonexistent.test    2013-10-08 22:44:20 +0900 (5bc4bf0)
@@ -0,0 +1,5 @@
+#@on-error omit
+register ruby/load
+#@on-error default
+
+ruby_load "nonexistent.rb"
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index