1/*
2 * Copyright (c) 2006-2008, The RubyCocoa Project.
3 * Copyright (c) 2001-2006, FUJIMOTO Hisakuni.
4 * All Rights Reserved.
5 *
6 * RubyCocoa is free software, covered under either the Ruby's license or the
7 * LGPL. See the COPYRIGHT file for more information.
8 */
9
10#import "ocexception.h"
11#import "ocdata_conv.h"
12#import "mdl_osxobjc.h"
13
14static VALUE
15_oc_exception_class(const char* name)
16{
17  VALUE mosx = rb_const_get(rb_cObject, rb_intern("OSX"));;
18  return rb_const_get(mosx, rb_intern(name));
19}
20
21VALUE
22ocdataconv_err_class(void)
23{
24  static VALUE exc = Qnil;
25  if (NIL_P(exc))
26    exc = _oc_exception_class("OCDataConvException");
27  return exc;
28}
29
30VALUE
31oc_err_class(void)
32{
33  static VALUE exc = Qnil;
34  if (NIL_P(exc))
35    exc = _oc_exception_class("OCException");
36  return exc;
37}
38
39VALUE
40ocmsgsend_err_class(void)
41{
42  static VALUE exc = Qnil;
43  if (NIL_P(exc))
44    exc = _oc_exception_class("OCMessageSendException");
45  return exc;
46}
47
48VALUE
49rb_err_new(VALUE klass, const char *fmt, ...)
50{
51  va_list args;
52  VALUE ret;
53  char buf[BUFSIZ];
54
55  va_start(args, fmt);
56  vsnprintf(buf, sizeof buf, fmt, args);
57  ret = rb_exc_new2(klass, buf);
58  va_end(args);
59  return ret;
60}
61
62VALUE
63oc_err_new (NSException* nsexcp)
64{
65  id pool;
66  char buf[BUFSIZ];
67
68  if ([[nsexcp name] hasPrefix: @"RBException_"]) {
69    // This is a wrapped Ruby exception
70    id rberr = [[nsexcp userInfo] objectForKey: @"$!"];
71    if (rberr) {
72      VALUE err = ocid_get_rbobj(rberr);
73      if (err != Qnil)
74        return err;
75    }
76  }
77
78  pool = [[NSAutoreleasePool alloc] init];
79  snprintf(buf, BUFSIZ, "%s - %s",
80	   [[nsexcp name] UTF8String], [[nsexcp reason] UTF8String]);
81  [pool release];
82  return rb_funcall(oc_err_class(), rb_intern("new"), 2, ocid_to_rbobj(Qnil, nsexcp), rb_str_new2(buf));
83}
84