Example #1
0
File: error.c Project: evan/ruby
static void
err_append(const char *s, rb_encoding *enc)
{
    rb_thread_t *th = GET_THREAD();
    VALUE err = th->errinfo;

    if (th->mild_compile_error) {
	if (!RTEST(err)) {
	    err = rb_exc_new3(rb_eSyntaxError,
			      rb_enc_str_new(s, strlen(s), enc));
	    th->errinfo = err;
	}
	else {
	    VALUE str = rb_obj_as_string(err);

	    rb_str_cat2(str, "\n");
	    rb_str_cat2(str, s);
	    th->errinfo = rb_exc_new3(rb_eSyntaxError, str);
	}
    }
    else {
	if (!RTEST(err)) {
	    err = rb_exc_new2(rb_eSyntaxError, "compile error");
	    th->errinfo = err;
	}
	rb_write_error(s);
	rb_write_error("\n");
    }
}
Example #2
0
static void
err_append(const char *s)
{
    rb_thread_t *th = GET_THREAD();
    VALUE err = th->errinfo;

    if (th->parse_in_eval) {
	if (!RTEST(err)) {
	    err = rb_exc_new2(rb_eSyntaxError, s);
	    th->errinfo = err;
	}
	else {
	    VALUE str = rb_obj_as_string(err);

	    rb_str_cat2(str, "\n");
	    rb_str_cat2(str, s);
	    th->errinfo = rb_exc_new3(rb_eSyntaxError, str);
	}
    }
    else {
	if (!RTEST(err)) {
	    err = rb_exc_new2(rb_eSyntaxError, "compile error");
	    th->errinfo = err;
	}
	rb_write_error(s);
	rb_write_error("\n");
    }
}
Example #3
0
File: error.c Project: knugie/ruby
static void
raise_loaderror(VALUE path, VALUE mesg)
{
    VALUE err = rb_exc_new3(rb_eLoadError, mesg);
    rb_ivar_set(err, rb_intern("@path"), path);
    rb_exc_raise(err);
}
Example #4
0
/*
 * Errors
 */
static VALUE
ossl_make_error(VALUE exc, const char *fmt, va_list args)
{
    VALUE str = Qnil;
    unsigned long e;

    if (fmt) {
	str = rb_vsprintf(fmt, args);
    }
    e = ERR_peek_last_error();
    if (e) {
	const char *msg = ERR_reason_error_string(e);

	if (NIL_P(str)) {
	    if (msg) str = rb_str_new_cstr(msg);
	}
	else {
	    if (RSTRING_LEN(str)) rb_str_cat2(str, ": ");
	    rb_str_cat2(str, msg ? msg : "(null)");
	}
	ossl_clear_error();
    }

    if (NIL_P(str)) str = rb_str_new(0, 0);
    return rb_exc_new3(exc, str);
}
Example #5
0
static void
raise_loaderror(VALUE path, VALUE mesg)
{
    VALUE err = rb_exc_new3(rb_eLoadError, mesg);
    rb_ivar_set(err, id_i_path, path);
    rb_exc_raise(err);
}
Example #6
0
static void
load_failed(VALUE fname)
{
    VALUE mesg = rb_str_buf_new_cstr("cannot load such file -- ");
    rb_str_append(mesg, fname);	/* should be ASCII compatible */
    rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
}
Example #7
0
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
  VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client));
  VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));
  VALUE e;
#ifdef HAVE_RUBY_ENCODING_H
  if (wrapper->server_version < 50500) {
    /* MySQL < 5.5 uses mixed encoding, just call it binary. */
    int err_enc = rb_ascii8bit_encindex();
    rb_enc_associate_index(rb_error_msg, err_enc);
    rb_enc_associate_index(rb_sql_state, err_enc);
  } else {
    /* MySQL >= 5.5 uses UTF-8 errors internally and converts them to the connection encoding. */
    rb_encoding *default_internal_enc = rb_default_internal_encoding();
    rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);
    rb_enc_associate(rb_error_msg, conn_enc);
    rb_enc_associate(rb_sql_state, conn_enc);
    if (default_internal_enc) {
      rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc);
      rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc);
    }
  }
#endif

  e = rb_exc_new3(cMysql2Error, rb_error_msg);
  rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client)));
  rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state);
  rb_exc_raise(e);
  return Qnil;
}
Example #8
0
static void
err_append(const char *s)
{

    if (rb_vm_parse_in_eval()) {
	VALUE err = rb_errinfo();
	if (err == Qnil) {
	    err = rb_exc_new2(rb_eSyntaxError, s);
	    rb_set_errinfo(err);
	}
	else {
	    VALUE str = rb_obj_as_string(err);

	    rb_str_cat2(str, "\n");
	    rb_str_cat2(str, s);
	    rb_set_errinfo(rb_exc_new3(rb_eSyntaxError, str));
	}
    }
    else {
	VALUE err = rb_vm_current_exception();
	if (err == Qnil) {
	    err = rb_exc_new2(rb_eSyntaxError, "compile error");
	    rb_vm_set_current_exception(err);
	}
	rb_write_error(s);
	rb_write_error("\n");
    }
}
Example #9
0
File: error.c Project: knugie/ruby
static void
compile_err_append(VALUE mesg)
{
    rb_thread_t *th = GET_THREAD();
    VALUE err = th->errinfo;
    rb_block_t *prev_base_block = th->base_block;
    th->base_block = 0;
    /* base_block should be zero while normal Ruby execution */
    /* after this line, any Ruby code *can* run */

    if (th->mild_compile_error) {
	if (RTEST(err)) {
	    VALUE str = rb_obj_as_string(err);

	    rb_str_cat2(str, "\n");
	    rb_str_append(str, mesg);
	    mesg = str;
	}
	err = rb_exc_new3(rb_eSyntaxError, mesg);
	th->errinfo = err;
    }
    else {
	if (!RTEST(err)) {
	    err = rb_exc_new2(rb_eSyntaxError, "compile error");
	    th->errinfo = err;
	}
	rb_str_cat2(mesg, "\n");
	rb_write_error_str(mesg);
    }

    /* returned to the parser world */
    th->base_block = prev_base_block;
}
Example #10
0
/*
 * call-seq:
 *    res.check -> nil
 *
 * Raises appropriate exception if PG::Result is in a bad state.
 */
VALUE
pg_result_check( VALUE self )
{
	VALUE error, exception, klass;
	VALUE rb_pgconn = rb_iv_get( self, "@connection" );
	PGconn *conn = pg_get_pgconn(rb_pgconn);
	PGresult *result;
#ifdef M17N_SUPPORTED
	rb_encoding *enc = pg_conn_enc_get( conn );
#endif
	char * sqlstate;

	Data_Get_Struct(self, PGresult, result);

	if(result == NULL)
	{
		error = rb_str_new2( PQerrorMessage(conn) );
	}
	else
	{
		switch (PQresultStatus(result))
		{
		case PGRES_TUPLES_OK:
		case PGRES_COPY_OUT:
		case PGRES_COPY_IN:
#ifdef HAVE_CONST_PGRES_COPY_BOTH
		case PGRES_COPY_BOTH:
#endif
#ifdef HAVE_CONST_PGRES_SINGLE_TUPLE
		case PGRES_SINGLE_TUPLE:
#endif
		case PGRES_EMPTY_QUERY:
		case PGRES_COMMAND_OK:
			return Qnil;
		case PGRES_BAD_RESPONSE:
		case PGRES_FATAL_ERROR:
		case PGRES_NONFATAL_ERROR:
			error = rb_str_new2( PQresultErrorMessage(result) );
			break;
		default:
			error = rb_str_new2( "internal error : unknown result status." );
		}
	}

#ifdef M17N_SUPPORTED
	rb_enc_set_index( error, rb_enc_to_index(enc) );
#endif

	sqlstate = PQresultErrorField( result, PG_DIAG_SQLSTATE );
	klass = lookup_error_class( sqlstate );
	exception = rb_exc_new3( klass, error );
	rb_iv_set( exception, "@connection", rb_pgconn );
	rb_iv_set( exception, "@result", self );
	rb_exc_raise( exception );

	/* Not reached */
	return Qnil;
}
Example #11
0
void rho_ruby_raise_argerror(const char *fmt, ...)
{
	va_list args;
	VALUE mesg;

	va_start(args, fmt);
	mesg = rb_vsprintf(fmt, args);
	va_end(args);
	rb_exc_raise(rb_exc_new3(rb_eArgError, mesg));
}
Example #12
0
File: error.c Project: knugie/ruby
void
rb_raise(VALUE exc, const char *fmt, ...)
{
    va_list args;
    VALUE mesg;

    va_start(args, fmt);
    mesg = rb_vsprintf(fmt, args);
    va_end(args);
    rb_exc_raise(rb_exc_new3(exc, mesg));
}
/*
 *  call-seq:
 *    transform(document, params = [])
 *
 *  Apply an XSLT stylesheet to an XML::Document.
 *  +params+ is an array of strings used as XSLT parameters.
 *  returns Nokogiri::XML::Document
 *
 *  Example:
 * 
 *    doc   = Nokogiri::XML(File.read(ARGV[0]))
 *    xslt  = Nokogiri::XSLT(File.read(ARGV[1]))
 *    puts xslt.transform(doc, ['key', 'value'])
 *
 */
static VALUE transform(int argc, VALUE* argv, VALUE self)
{
    VALUE xmldoc, paramobj, errstr, exception ;
    xmlDocPtr xml ;
    xmlDocPtr result ;
    nokogiriXsltStylesheetTuple *wrapper;
    const char** params ;
    long param_len, j ;
    int parse_error_occurred ;

    rb_scan_args(argc, argv, "11", &xmldoc, &paramobj);
    if (NIL_P(paramobj)) { paramobj = rb_ary_new2(0L) ; }
    if (!rb_obj_is_kind_of(xmldoc, cNokogiriXmlDocument))
      rb_raise(rb_eArgError, "argument must be a Nokogiri::XML::Document");

    /* handle hashes as arguments. */
    if(T_HASH == TYPE(paramobj)) {
      paramobj = rb_funcall(paramobj, rb_intern("to_a"), 0);
      paramobj = rb_funcall(paramobj, rb_intern("flatten"), 0);
    }

    Check_Type(paramobj, T_ARRAY);

    Data_Get_Struct(xmldoc, xmlDoc, xml);
    Data_Get_Struct(self, nokogiriXsltStylesheetTuple, wrapper);

    param_len = RARRAY_LEN(paramobj);
    params = calloc((size_t)param_len+1, sizeof(char*));
    for (j = 0 ; j < param_len ; j++) {
      VALUE entry = rb_ary_entry(paramobj, j);
      const char * ptr = StringValuePtr(entry);
      params[j] = ptr;
    }
    params[param_len] = 0 ;

    errstr = rb_str_new(0, 0);
    xsltSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);
    xmlSetGenericErrorFunc(NULL, (xmlGenericErrorFunc)&swallow_superfluous_xml_errors);

    result = xsltApplyStylesheet(wrapper->ss, xml, params);
    free(params);

    xsltSetGenericErrorFunc(NULL, NULL);
    xmlSetGenericErrorFunc(NULL, NULL);

    parse_error_occurred = (Qfalse == rb_funcall(errstr, rb_intern("empty?"), 0));

    if (parse_error_occurred) {
      exception = rb_exc_new3(rb_eRuntimeError, errstr);
      rb_exc_raise(exception);
    }

    return Nokogiri_wrap_xml_document((VALUE)0, result) ;
}
Example #14
0
void
rb_loaderror(const char *fmt, ...)
{
    va_list args;
    VALUE mesg;

    va_start(args, fmt);
    mesg = rb_vsprintf(fmt, args);
    va_end(args);
    rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
}
Example #15
0
File: error.c Project: knugie/ruby
void
rb_fatal(const char *fmt, ...)
{
    va_list args;
    VALUE mesg;

    va_start(args, fmt);
    mesg = rb_vsprintf(fmt, args);
    va_end(args);

    rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
}
Example #16
0
static VALUE
make_error_str(VALUE klass, const char* fmt, ...)
{
	VALUE exc;
	VALUE str;

	va_list va;
	va_start(va, fmt);
	str = rb_vsprintf(fmt, va);
	va_end(va);
	exc = rb_exc_new3(klass, str);
	rb_str_free(str);
	return exc;
}
Example #17
0
/*
 * call-seq:
 *    res.check -> nil
 *
 * Raises appropriate exception if PG::Result is in a bad state.
 */
VALUE
pg_result_check( VALUE self )
{
	t_pg_result *this = pgresult_get_this(self);
	VALUE error, exception, klass;
	char * sqlstate;

	if(this->pgresult == NULL)
	{
		PGconn *conn = pg_get_pgconn(this->connection);
		error = rb_str_new2( PQerrorMessage(conn) );
	}
	else
	{
		switch (PQresultStatus(this->pgresult))
		{
		case PGRES_TUPLES_OK:
		case PGRES_COPY_OUT:
		case PGRES_COPY_IN:
#ifdef HAVE_CONST_PGRES_COPY_BOTH
		case PGRES_COPY_BOTH:
#endif
#ifdef HAVE_CONST_PGRES_SINGLE_TUPLE
		case PGRES_SINGLE_TUPLE:
#endif
		case PGRES_EMPTY_QUERY:
		case PGRES_COMMAND_OK:
			return self;
		case PGRES_BAD_RESPONSE:
		case PGRES_FATAL_ERROR:
		case PGRES_NONFATAL_ERROR:
			error = rb_str_new2( PQresultErrorMessage(this->pgresult) );
			break;
		default:
			error = rb_str_new2( "internal error : unknown result status." );
		}
	}

	PG_ENCODING_SET_NOCHECK( error, ENCODING_GET(self) );

	sqlstate = PQresultErrorField( this->pgresult, PG_DIAG_SQLSTATE );
	klass = lookup_error_class( sqlstate );
	exception = rb_exc_new3( klass, error );
	rb_iv_set( exception, "@connection", this->connection );
	rb_iv_set( exception, "@result", this->pgresult ? self : Qnil );
	rb_exc_raise( exception );

	/* Not reached */
	return self;
}
Example #18
0
VALUE
rb_make_exception(int argc, VALUE *argv)
{
    VALUE mesg;
    ID exception;
    int n;

    mesg = Qnil;
    switch (argc) {
      case 0:
	//mesg = Qnil;
	mesg = rb_exc_new2(rb_eRuntimeError, "");
	break;
      case 1:
	if (NIL_P(argv[0])) {
	    break;
	}
	if (TYPE(argv[0]) == T_STRING) {
	    mesg = rb_exc_new3(rb_eRuntimeError, argv[0]);
	    break;
	}
	n = 0;
	goto exception_call;

      case 2:
      case 3:
	n = 1;
      exception_call:
	exception = rb_intern("exception");
	if (!rb_respond_to(argv[0], exception)) {
	    rb_raise(rb_eTypeError, "exception class/object expected");
	}
	mesg = rb_funcall(argv[0], exception, n, argv[1]);
	break;
      default:
	rb_raise(rb_eArgError, "wrong number of arguments");
	break;
    }
    if (argc > 0) {
	if (!rb_obj_is_kind_of(mesg, rb_eException))
	    rb_raise(rb_eTypeError, "exception object expected");
	if (argc > 2)
	    set_backtrace(mesg, argv[2]);
    }

    return mesg;
}
Example #19
0
static void
err_append(const char *s)
{
  rb_thread_t *th = GET_THREAD();
  if (th->parse_in_eval) {
    if (NIL_P(th->errinfo)) {
      th->errinfo = rb_exc_new2(rb_eSyntaxError, s);
    }
    else {
      VALUE str = rb_obj_as_string(GET_THREAD()->errinfo);

      rb_str_cat2(str, "\n");
      rb_str_cat2(str, s);
      th->errinfo = rb_exc_new3(rb_eSyntaxError, str);
    }
  }
  else {
    rb_write_error(s);
    rb_write_error("\n");
  }
}
Example #20
0
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) {
  VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client));
  VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client));
#ifdef HAVE_RUBY_ENCODING_H
  rb_encoding *default_internal_enc = rb_default_internal_encoding();
  rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding);

  rb_enc_associate(rb_error_msg, conn_enc);
  rb_enc_associate(rb_sql_state, conn_enc);
  if (default_internal_enc) {
    rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc);
    rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc);
  }
#endif

  VALUE e = rb_exc_new3(cMysql2Error, rb_error_msg);
  rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client)));
  rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state);
  rb_exc_raise(e);
  return Qnil;
}
Example #21
0
static void
err_append(const char *s)
{
    extern VALUE ruby_errinfo;

    if (ruby_in_eval) {
	if (NIL_P(ruby_errinfo)) {
	    ruby_errinfo = rb_exc_new2(rb_eSyntaxError, s);
	}
	else {
	    VALUE str = rb_obj_as_string(ruby_errinfo);

	    rb_str_cat2(str, "\n");
	    rb_str_cat2(str, s);
	    ruby_errinfo = rb_exc_new3(rb_eSyntaxError, str);
	}
    }
    else {
	rb_write_error(s);
	rb_write_error("\n");
    }
}
Example #22
0
File: ossl.c Project: DashYang/sim
/*
 * Errors
 */
static VALUE
ossl_make_error(VALUE exc, const char *fmt, va_list args)
{
    VALUE str = Qnil;
    const char *msg;
    long e;

#ifdef HAVE_ERR_PEEK_LAST_ERROR
    e = ERR_peek_last_error();
#else
    e = ERR_peek_error();
#endif
    if (fmt) {
	str = rb_vsprintf(fmt, args);
    }
    if (e) {
	if (dOSSL == Qtrue) /* FULL INFO */
	    msg = ERR_error_string(e, NULL);
	else
	    msg = ERR_reason_error_string(e);
	if (NIL_P(str)) {
	    if (msg) str = rb_str_new_cstr(msg);
	}
	else {
	    if (RSTRING_LEN(str)) rb_str_cat2(str, ": ");
	    rb_str_cat2(str, msg ? msg : "(null)");
	}
    }
    if (dOSSL == Qtrue){ /* show all errors on the stack */
	while ((e = ERR_get_error()) != 0){
	    rb_warn("error on stack: %s", ERR_error_string(e, NULL));
	}
    }
    ERR_clear_error();

    if (NIL_P(str)) str = rb_str_new(0, 0);
    return rb_exc_new3(exc, str);
}
Example #23
0
static void
argument_error(const rb_iseq_t *iseq, int miss_argc, int correct_argc)
{
    VALUE mesg = rb_sprintf("wrong number of arguments (%d for %d)", miss_argc, correct_argc);
    VALUE exc = rb_exc_new3(rb_eArgError, mesg);
    VALUE bt = rb_make_backtrace();
    VALUE err_line = 0;

    if (iseq) {
	int line_no = 1;

	if (iseq->insn_info_size) {
	    line_no = iseq->insn_info_table[0].line_no;
	}

	err_line = rb_sprintf("%s:%d:in `%s'",
			      RSTRING_PTR(iseq->filename),
			      line_no, RSTRING_PTR(iseq->name));
	rb_funcall(bt, rb_intern("unshift"), 1, err_line);
    }

    rb_funcall(exc, rb_intern("set_backtrace"), 1, bt);
    rb_exc_raise(exc);
}
/*
 * call-seq:
 *   parse_stylesheet_doc(document)
 *
 * Parse a stylesheet from +document+.
 */
static VALUE parse_stylesheet_doc(VALUE klass, VALUE xmldocobj)
{
    xmlDocPtr xml, xml_cpy;
    VALUE errstr, exception;
    xsltStylesheetPtr ss ;
    Data_Get_Struct(xmldocobj, xmlDoc, xml);
    exsltRegisterAll();

    errstr = rb_str_new(0, 0);
    xsltSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);

    xml_cpy = xmlCopyDoc(xml, 1); /* 1 => recursive */
    ss = xsltParseStylesheetDoc(xml_cpy);

    xsltSetGenericErrorFunc(NULL, NULL);

    if (!ss) {
	xmlFreeDoc(xml_cpy);
	exception = rb_exc_new3(rb_eRuntimeError, errstr);
	rb_exc_raise(exception);
    }

    return Nokogiri_wrap_xslt_stylesheet(ss);
}
Example #25
0
void *oci8_find_symbol(const char *symbol_name)
{
#if defined _WIN32
    /* Windows */
    static HMODULE hModule = NULL;

    if (hModule == NULL) {
        hModule = LoadLibrary("OCI.DLL");
        if (hModule == NULL) {
            char message[512];
            int error = GetLastError();
            char *p;

            memset(message, 0, sizeof(message));
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), message, sizeof(message), NULL);
            for (p = message; *p; p++) {
                if (*p == '\n' || *p == '\r')
                    *p = ' ';
            }
            rb_raise(rb_eLoadError, "OCI.DLL: %d(%s)", error, message);
        }
    }
    return GetProcAddress(hModule, symbol_name);
#else
    /* UNIX */
    static void *handle = NULL;

    if (handle == NULL) {
        static const char * const sonames[] = {
#if defined(__CYGWIN__)
            /* Windows(Cygwin) */
            "OCI.DLL",
#elif defined(_AIX)
            /* AIX */
            "libclntsh.a(shr.o)",
#elif defined(__hppa)
            /* HP-UX(PA-RISC) */
            "libclntsh.sl.11.1",
            "libclntsh.sl.10.1",
            "libclntsh.sl.9.0",
            "libclntsh.sl.8.0",
#elif defined(__APPLE__)
            /* Mac OS X */
            "libclntsh.dylib.11.1",
            "libclntsh.dylib.10.1",
#else
            /* Linux, Solaris and HP-UX(IA64) */
            "libclntsh.so.11.1",
            "libclntsh.so.10.1",
            "libclntsh.so.9.0",
            "libclntsh.so.8.0",
#endif
        };
#define NUM_SONAMES (sizeof(sonames)/sizeof(sonames[0]))
        size_t idx;
        VALUE err = rb_ary_new();

#ifdef _AIX
#define DLOPEN_FLAG (RTLD_LAZY|RTLD_GLOBAL|RTLD_MEMBER)
#else
#define DLOPEN_FLAG (RTLD_LAZY|RTLD_GLOBAL)
#endif
        for (idx = 0; idx < NUM_SONAMES; idx++) {
            handle = dlopen(sonames[idx], DLOPEN_FLAG);
            if (handle != NULL) {
                break;
            }
            rb_ary_push(err, rb_locale_str_new_cstr(dlerror()));
        }
        if (handle == NULL) {
            VALUE msg;

            msg = rb_str_buf_new(NUM_SONAMES * 50);
            for (idx = 0; idx < NUM_SONAMES; idx++) {
                const char *errmsg = RSTRING_PTR(RARRAY_PTR(err)[idx]);
                if (idx != 0) {
                    rb_str_buf_cat2(msg, " ");
                }
                if (strstr(errmsg, sonames[idx]) == NULL) {
                    /* prepend "soname: " if soname is not found in
                     * the error message.
                     */
                    rb_str_buf_cat2(msg, sonames[idx]);
                    rb_str_buf_cat2(msg, ": ");
                }
                rb_str_buf_append(msg, RARRAY_PTR(err)[idx]);
                rb_str_buf_cat2(msg, ";");
            }
            rb_exc_raise(rb_exc_new3(rb_eLoadError, msg));
        }
    }
    return dlsym(handle, symbol_name);
#endif /* defined _WIN32 */
}
Example #26
0
VALUE exception_spec_rb_exc_new3(VALUE self, VALUE str) {
  return rb_exc_new3(rb_eException, str);
}
Example #27
0
    void
cb_get_callback(lcb_t handle, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp)
{
    struct cb_context_st *ctx = (struct cb_context_st *)cookie;
    struct cb_bucket_st *bucket = ctx->bucket;
    VALUE key, val, flags, cas, exc = Qnil, res, raw;

    ctx->nqueries--;
    key = STR_NEW((const char*)resp->v.v0.key, resp->v.v0.nkey);
    cb_strip_key_prefix(bucket, key);

    if (error != LCB_KEY_ENOENT || !ctx->quiet) {
        exc = cb_check_error(error, "failed to get value", key);
        if (exc != Qnil) {
            rb_ivar_set(exc, cb_id_iv_operation, cb_sym_get);
            ctx->exception = exc;
        }
    }

    flags = ULONG2NUM(resp->v.v0.flags);
    cas = ULL2NUM(resp->v.v0.cas);
    raw = STR_NEW((const char*)resp->v.v0.bytes, resp->v.v0.nbytes);
    val = cb_decode_value(ctx->transcoder, raw, resp->v.v0.flags, ctx->transcoder_opts);
    if (rb_obj_is_kind_of(val, rb_eStandardError)) {
        VALUE exc_str = rb_funcall(val, cb_id_to_s, 0);
        VALUE msg = rb_funcall(rb_mKernel, cb_id_sprintf, 3,
                rb_str_new2("unable to convert value for key \"%s\": %s"), key, exc_str);
        ctx->exception = rb_exc_new3(cb_eValueFormatError, msg);
        rb_ivar_set(ctx->exception, cb_id_iv_operation, cb_sym_get);
        rb_ivar_set(ctx->exception, cb_id_iv_key, key);
        rb_ivar_set(ctx->exception, cb_id_iv_inner_exception, val);
        val = Qnil;
    }
    if (bucket->async) { /* asynchronous */
        if (ctx->proc != Qnil) {
            res = rb_class_new_instance(0, NULL, cb_cResult);
            rb_ivar_set(res, cb_id_iv_error, exc);
            rb_ivar_set(res, cb_id_iv_operation, cb_sym_get);
            rb_ivar_set(res, cb_id_iv_key, key);
            rb_ivar_set(res, cb_id_iv_value, val);
            rb_ivar_set(res, cb_id_iv_flags, flags);
            rb_ivar_set(res, cb_id_iv_cas, cas);
            cb_proc_call(bucket, ctx->proc, 1, res);
        }
    } else {                /* synchronous */
        if (NIL_P(exc) && error != LCB_KEY_ENOENT) {
            if (ctx->extended) {
                val = rb_ary_new3(3, val, flags, cas);
            }
            if (ctx->all_replicas) {
                VALUE ary = rb_hash_aref(ctx->rv, key);
                if (NIL_P(ary)) {
                    ary = rb_ary_new();
                    rb_hash_aset(ctx->rv, key, ary);
                }
                rb_ary_push(ary, val);
            } else {
                rb_hash_aset(ctx->rv, key, val);
            }
        }
    }

    if (ctx->nqueries == 0) {
        ctx->proc = Qnil;
        if (bucket->async) {
            cb_context_free(ctx);
        }
    }
    (void)handle;
}
Example #28
0
void
Init_Proc(void)
{
    /* Proc */
    rb_cProc = rb_define_class("Proc", rb_cObject);
    rb_undef_alloc_func(rb_cProc);
    rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1);
    rb_define_method(rb_cProc, "call", proc_call, -1);
    rb_define_method(rb_cProc, "[]", proc_call, -1);
    rb_define_method(rb_cProc, "===", proc_call, -1);
    rb_define_method(rb_cProc, "yield", proc_call, -1);
    rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
    rb_define_method(rb_cProc, "arity", proc_arity, 0);
    rb_define_method(rb_cProc, "clone", proc_clone, 0);
    rb_define_method(rb_cProc, "dup", proc_dup, 0);
    rb_define_method(rb_cProc, "==", proc_eq, 1);
    rb_define_method(rb_cProc, "eql?", proc_eq, 1);
    rb_define_method(rb_cProc, "hash", proc_hash, 0);
    rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
    rb_define_method(rb_cProc, "lambda?", proc_lambda_p, 0);
    rb_define_method(rb_cProc, "binding", proc_binding, 0);
    rb_define_method(rb_cProc, "curry", proc_curry, -1);
    rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);

    /* Exceptions */
    rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
    rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0);
    rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0);

    rb_eSysStackError = rb_define_class("SystemStackError", rb_eException);
    sysstack_error = rb_exc_new3(rb_eSysStackError,
				 rb_obj_freeze(rb_str_new2("stack level too deep")));
    OBJ_TAINT(sysstack_error);
    OBJ_FREEZE(sysstack_error);

    /* utility functions */
    rb_define_global_function("proc", rb_block_proc, 0);
    rb_define_global_function("lambda", proc_lambda, 0);

    /* Method */
    rb_cMethod = rb_define_class("Method", rb_cObject);
    rb_undef_alloc_func(rb_cMethod);
    rb_undef_method(CLASS_OF(rb_cMethod), "new");
    rb_define_method(rb_cMethod, "==", method_eq, 1);
    rb_define_method(rb_cMethod, "eql?", method_eq, 1);
    rb_define_method(rb_cMethod, "hash", method_hash, 0);
    rb_define_method(rb_cMethod, "clone", method_clone, 0);
    rb_define_method(rb_cMethod, "call", rb_method_call, -1);
    rb_define_method(rb_cMethod, "[]", rb_method_call, -1);
    rb_define_method(rb_cMethod, "arity", method_arity_m, 0);
    rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
    rb_define_method(rb_cMethod, "to_s", method_inspect, 0);
    rb_define_method(rb_cMethod, "to_proc", method_proc, 0);
    rb_define_method(rb_cMethod, "receiver", method_receiver, 0);
    rb_define_method(rb_cMethod, "name", method_name, 0);
    rb_define_method(rb_cMethod, "owner", method_owner, 0);
    rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
    rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
    rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
    rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);

    /* UnboundMethod */
    rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject);
    rb_undef_alloc_func(rb_cUnboundMethod);
    rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new");
    rb_define_method(rb_cUnboundMethod, "==", method_eq, 1);
    rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1);
    rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0);
    rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0);
    rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0);
    rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0);
    rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0);
    rb_define_method(rb_cUnboundMethod, "name", method_name, 0);
    rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0);
    rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
    rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);

    /* Module#*_method */
    rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
    rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1);
    rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1);

    /* Kernel */
    rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1);
}
Example #29
0
/* Helper to convert return code from libcouchbase to meaningful exception.
 * Returns nil if the code considering successful and exception object
 * otherwise. Store given string to exceptions as message, and also
 * initialize +error+ attribute with given return code.  */
    VALUE
cb_check_error_with_status(lcb_error_t rc, const char *msg, VALUE key,
        lcb_http_status_t status)
{
    VALUE klass, exc, str;
    char buf[300];

    if ((rc == LCB_SUCCESS && (status == 0 || status / 100 == 2)) ||
            rc == LCB_AUTH_CONTINUE) {
        return Qnil;
    }
    switch (rc) {
        case LCB_AUTH_ERROR:
            klass = cb_eAuthError;
            break;
        case LCB_DELTA_BADVAL:
            klass = cb_eDeltaBadvalError;
            break;
        case LCB_E2BIG:
            klass = cb_eTooBigError;
            break;
        case LCB_EBUSY:
            klass = cb_eBusyError;
            break;
        case LCB_EINTERNAL:
            klass = cb_eInternalError;
            break;
        case LCB_EINVAL:
            klass = cb_eInvalidError;
            break;
        case LCB_ENOMEM:
            klass = cb_eNoMemoryError;
            break;
        case LCB_ERANGE:
            klass = cb_eRangeError;
            break;
        case LCB_ETMPFAIL:
            klass = cb_eTmpFailError;
            break;
        case LCB_KEY_EEXISTS:
            klass = cb_eKeyExistsError;
            break;
        case LCB_KEY_ENOENT:
            klass = cb_eNotFoundError;
            break;
        case LCB_DLOPEN_FAILED:
            klass = cb_eDlopenFailedError;
            break;
        case LCB_DLSYM_FAILED:
            klass = cb_eDlsymFailedError;
            break;
        case LCB_NETWORK_ERROR:
            klass = cb_eNetworkError;
            break;
        case LCB_NOT_MY_VBUCKET:
            klass = cb_eNotMyVbucketError;
            break;
        case LCB_NOT_STORED:
            klass = cb_eNotStoredError;
            break;
        case LCB_NOT_SUPPORTED:
            klass = cb_eNotSupportedError;
            break;
        case LCB_UNKNOWN_COMMAND:
            klass = cb_eUnknownCommandError;
            break;
        case LCB_UNKNOWN_HOST:
            klass = cb_eUnknownHostError;
            break;
        case LCB_PROTOCOL_ERROR:
            klass = cb_eProtocolError;
            break;
        case LCB_ETIMEDOUT:
            klass = cb_eTimeoutError;
            break;
        case LCB_CONNECT_ERROR:
            klass = cb_eConnectError;
            break;
        case LCB_BUCKET_ENOENT:
            klass = cb_eBucketNotFoundError;
            break;
        case LCB_CLIENT_ENOMEM:
            klass = cb_eClientNoMemoryError;
            break;
        case LCB_CLIENT_ETMPFAIL:
            klass = cb_eClientTmpFailError;
            break;
        case LCB_EBADHANDLE:
            klass = cb_eBadHandleError;
            break;
        case LCB_ERROR:
            /* fall through */
        default:
            klass = cb_eLibcouchbaseError;
    }

    str = rb_str_buf_new2(msg ? msg : "");
    rb_str_buf_cat2(str, " (");
    if (key != Qnil) {
        snprintf(buf, 300, "key=\"%s\", ", RSTRING_PTR(key));
        rb_str_buf_cat2(str, buf);
    }
    if (status > 0) {
        const char *reason = NULL;
        klass = cb_eHTTPError;
        snprintf(buf, 300, "status=\"%d\"", status);
        rb_str_buf_cat2(str, buf);
        switch (status) {
            case LCB_HTTP_STATUS_BAD_REQUEST:
                reason = " (Bad Request)";
                break;
            case LCB_HTTP_STATUS_UNAUTHORIZED:
                reason = " (Unauthorized)";
                break;
            case LCB_HTTP_STATUS_PAYMENT_REQUIRED:
                reason = " (Payment Required)";
                break;
            case LCB_HTTP_STATUS_FORBIDDEN:
                reason = " (Forbidden)";
                break;
            case LCB_HTTP_STATUS_NOT_FOUND:
                reason = " (Not Found)";
                break;
            case LCB_HTTP_STATUS_METHOD_NOT_ALLOWED:
                reason = " (Method Not Allowed)";
                break;
            case LCB_HTTP_STATUS_NOT_ACCEPTABLE:
                reason = " (Not Acceptable)";
                break;
            case LCB_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED:
                reason = " (Proxy Authentication Required)";
                break;
            case LCB_HTTP_STATUS_REQUEST_TIMEOUT:
                reason = " (Request Timeout)";
                break;
            case LCB_HTTP_STATUS_CONFLICT:
                reason = " (Conflict)";
                break;
            case LCB_HTTP_STATUS_GONE:
                reason = " (Gone)";
                break;
            case LCB_HTTP_STATUS_LENGTH_REQUIRED:
                reason = " (Length Required)";
                break;
            case LCB_HTTP_STATUS_PRECONDITION_FAILED:
                reason = " (Precondition Failed)";
                break;
            case LCB_HTTP_STATUS_REQUEST_ENTITY_TOO_LARGE:
                reason = " (Request Entity Too Large)";
                break;
            case LCB_HTTP_STATUS_REQUEST_URI_TOO_LONG:
                reason = " (Request Uri Too Long)";
                break;
            case LCB_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE:
                reason = " (Unsupported Media Type)";
                break;
            case LCB_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE:
                reason = " (Requested Range Not Satisfiable)";
                break;
            case LCB_HTTP_STATUS_EXPECTATION_FAILED:
                reason = " (Expectation Failed)";
                break;
            case LCB_HTTP_STATUS_UNPROCESSABLE_ENTITY:
                reason = " (Unprocessable Entity)";
                break;
            case LCB_HTTP_STATUS_LOCKED:
                reason = " (Locked)";
                break;
            case LCB_HTTP_STATUS_FAILED_DEPENDENCY:
                reason = " (Failed Dependency)";
                break;
            case LCB_HTTP_STATUS_INTERNAL_SERVER_ERROR:
                reason = " (Internal Server Error)";
                break;
            case LCB_HTTP_STATUS_NOT_IMPLEMENTED:
                reason = " (Not Implemented)";
                break;
            case LCB_HTTP_STATUS_BAD_GATEWAY:
                reason = " (Bad Gateway)";
                break;
            case LCB_HTTP_STATUS_SERVICE_UNAVAILABLE:
                reason = " (Service Unavailable)";
                break;
            case LCB_HTTP_STATUS_GATEWAY_TIMEOUT:
                reason = " (Gateway Timeout)";
                break;
            case LCB_HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED:
                reason = " (Http Version Not Supported)";
                break;
            case LCB_HTTP_STATUS_INSUFFICIENT_STORAGE:
                reason = " (Insufficient Storage)";
                break;
            default:
                reason = "";
        }
        rb_str_buf_cat2(str, reason);
        rb_str_buf_cat2(str, ", ");

    }
    snprintf(buf, 300, "error=0x%02x)", rc);
    rb_str_buf_cat2(str, buf);
    exc = rb_exc_new3(klass, str);
    rb_ivar_set(exc, cb_id_iv_error, INT2FIX(rc));
    rb_ivar_set(exc, cb_id_iv_key, key);
    rb_ivar_set(exc, cb_id_iv_cas, Qnil);
    rb_ivar_set(exc, cb_id_iv_operation, Qnil);
    rb_ivar_set(exc, cb_id_iv_status, status ? INT2FIX(status) : Qnil);
    return exc;
}