# Howto make something like Errno::##, for my C ext, or do I need to?

**URL:** <https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493>\
**Category:** ruby-talk\
**Created:** [18 May 2005 19:08 UTC](https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493 "2005-05-18T19:08:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [18 May 2005 19:08 UTC](https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493/1 "2005-05-18T19:08:52Z")

</div>

Doesn't rb\_sys\_fail(0) work for this scenario? I thought if a C  
function failed, you could call rb\_sys\_fail(0) and it would raise the  
appropriate Errno. Or am I confused?

Dan

> **···**
>
> > -----Original Message-----  
> > From: Charles Mills [mailto:cmills@freeshell.org]  
> > Sent: Wednesday, May 18, 2005 12:20 PM  
> > To: ruby-talk ML  
> > Subject: Re: howto make something like Errno::##, for my C  
> > ext, or do I need to?
> > 
> > Sam Roberts wrote:  
> > \> I'm wrapping a library, it returns its errors as numbers (many of  
> > them,  
> > \> too many to wrap even automatically), and I don't know how to raise  
> > \> exceptions.  
> > \>  
> > \> I've looked at how ruby deals with unix error numbers. I  
> > guess I could  
> > \> cut-n-paste all the code to that my extension does the same...  
> > but  
> > \> that seems wrong, somehow.  
> > \>  
> > \> What I'd really like to do is have only one error class, but raise  
> > \> objects as exceptions, with the objects @errno set to the  
> > value, I'd  
> > \> do this in ruby like this:  
> > \>  
> > \> class MyErr \< StandardError  
> > \> attr\_reader :eno  
> > \> def initialize(eno); @eno = eno; end  
> > \> end  
> > \>  
> > \>  
> > \> ...  
> > \> raise MyError.new(35)  
> > \>  
> > \> Looking at the exception raising APIs in README.EXT, I can't quite  
> > see  
> > \> how to do this.  
> > \>
> > 
> > In short:  
> > rb\_exc\_raise(my\_err\_new(eno));
> > 
> > You probably want to have the ability to add a message to  
> > your error objects so you may want to do:  
> > rb\_exc\_raise(my\_err\_new("msg", eno));
> > 
> > or define a function like
> > 
> > static void  
> > raise\_my\_err(int eno, const char \*fmt, ...)  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;va\_list args;  
> > &nbsp;&nbsp;&nbsp;&nbsp;char buf[BUFSIZ];
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;va\_init\_list(args, fmt);  
> > &nbsp;&nbsp;&nbsp;&nbsp;vsnprintf(buf, BUFSIZ, fmt, args);  
> > &nbsp;&nbsp;&nbsp;&nbsp;va\_end(args);  
> > &nbsp;&nbsp;&nbsp;&nbsp;rb\_exc\_raise(my\_err\_new(buf, eno));  
> > }
> > 
> > most of the above is a copy and paste from code in error.c.  
> > I think your new function would look something like this:
> > 
> > static VALUE  
> > my\_err\_new(const char \*buf, int eno)  
> > {  
> > &nbsp;&nbsp;VALUE self = rb\_exc\_new2(cMyErr, buf);  
> > &nbsp;&nbsp;rb\_iv\_set(self, "@eno", INT2FIX(eno));  
> > &nbsp;&nbsp;return self;  
> > }
> > 
> > -Charlie

---

<div class="post-metadata">

**Author:** ![Charles\_Mills1](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Charles\_Mills1](https://rubytalk.org/u/Charles_Mills1)\
**Post date:** [18 May 2005 19:20 UTC](https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493/2 "2005-05-18T19:20:13Z")

</div>

Berger, Daniel wrote:

> \> From: Charles Mills [mailto:cmills@freeshell.org]  
> \> Sent: Wednesday, May 18, 2005 12:20 PM  
> \> To: ruby-talk ML  
> \> Subject: Re: howto make something like Errno::##, for my C  
> \> ext, or do I need to?  
> \>  
> \>  
> \>  
> \> Sam Roberts wrote:  
> \> \> I'm wrapping a library, it returns its errors as numbers (many of  
> \> them,  
> \> \> too many to wrap even automatically), and I don't know how to

raise

> \> \> exceptions.  
> \> \>  
> \> \> I've looked at how ruby deals with unix error numbers. I  
> \> guess I could  
> \> \> cut-n-paste all the code to that my extension does the same...  
> \> but  
> \> \> that seems wrong, somehow.  
> \> \>  
> \> \> What I'd really like to do is have only one error class, but

raise

> \> \> objects as exceptions, with the objects @errno set to the  
> \> value, I'd  
> \> \> do this in ruby like this:  
> \> \>  
> \> \> class MyErr \< StandardError  
> \> \> attr\_reader :eno  
> \> \> def initialize(eno); @eno = eno; end  
> \> \> end  
> \> \>  
> \> \>  
> \> \> ...  
> \> \> raise MyError.new(35)  
> \> \>  
> \> \> Looking at the exception raising APIs in README.EXT, I can't

quite

> \> see  
> \> \> how to do this.  
> \> \>  
> \>  
> \> In short:  
> \> rb\_exc\_raise(my\_err\_new(eno));  
> \>  
> \> You probably want to have the ability to add a message to  
> \> your error objects so you may want to do:  
> \> rb\_exc\_raise(my\_err\_new("msg", eno));  
> \>  
> \> or define a function like  
> \>  
> \> static void  
> \> raise\_my\_err(int eno, const char \*fmt, ...)  
> \> {  
> \> va\_list args;  
> \> char buf[BUFSIZ];  
> \>  
> \> va\_init\_list(args, fmt);  
> \> vsnprintf(buf, BUFSIZ, fmt, args);  
> \> va\_end(args);  
> \> rb\_exc\_raise(my\_err\_new(buf, eno));  
> \> }  
> \>  
> \> most of the above is a copy and paste from code in error.c.  
> \> I think your new function would look something like this:  
> \>  
> \> static VALUE  
> \> my\_err\_new(const char \*buf, int eno)  
> \> {  
> \> VALUE self = rb\_exc\_new2(cMyErr, buf);  
> \> rb\_iv\_set(self, "@eno", INT2FIX(eno));  
> \> return self;  
> \> }  
> \>  
> \> -Charlie
> 
> Doesn't rb\_sys\_fail(0) work for this scenario? I thought if a C  
> function failed, you could call rb\_sys\_fail(0) and it would raise the  
> appropriate Errno. Or am I confused?
> 
> Dan

Yeah that works too, if your using the standard library functions. I  
thought from the OP that the error numbers he was working with were not  
related to those used by the std lib, but I may be wrong.

-Charlie

> **···**
>
> > \> -----Original Message-----

---

<div class="post-metadata">

**Author:** ![Sam\_Roberts](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sam\_Roberts](https://rubytalk.org/u/Sam_Roberts)\
**Post date:** [18 May 2005 21:28 UTC](https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493/3 "2005-05-18T21:28:04Z")

</div>

Thanks for your help, Charlie.

Charlie Mills wrote:

> In short:  
> rb\_exc\_raise(my\_err\_new(eno));

Took me a minute to find that... it's in intern.h, can I call those  
functions, I mean, should I?

Looking through that code, it seems my only option is to create the  
object myself, then call this internal function.

Looks like it will work fine, but is this really recommended?

Daniel wrote:

> Doesn't rb\_sys\_fail(0) work for this scenario? I thought if a C  
> function failed, you could call rb\_sys\_fail(0) and it would raise the  
> appropriate Errno. Or am I confused?

I didn't know about this function, but no, its not for me.

It only makes sense if the C function you are calling is returning a  
system error number, i.e. from \<errno.h\>.

For example, when a C function in our library returns 9, it means "index  
out of bounds", but when a system call returns 9 it means bad file  
descriptor, and when the Crypoki API (random example) returns 9 it means  
CKR\_NEED\_TO\_CREATE\_THREADS.

I wouldn't want to return Errno::EBADF from my wrapper when the error is  
index out of bounds!

Charlie wrote:

> Yeah that works too, if your using the standard library functions. I  
> thought from the OP that the error numbers he was working with were not  
> related to those used by the std lib, but I may be wrong.

Nope, bang on.

Sam

> **···**
>
> --
> 
> > **[BlackBerry Certicom Official Home Page - Protecting content, applications and...](https://blackberry.certicom.com/en)**
> >
> > With over 35 years of experience in applied cryptography, Certicom provides PKI and key management solutions to help secure the automotive, semiconductors and IoT industry.

---

<div class="post-metadata">

**Author:** ![Charles\_Mills1](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Charles\_Mills1](https://rubytalk.org/u/Charles_Mills1)\
**Post date:** [18 May 2005 22:30 UTC](https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493/4 "2005-05-18T22:30:13Z")

</div>

Sam Roberts wrote:

> Thanks for your help, Charlie.
> 
> Charlie Mills wrote:  
> \> In short:  
> \> rb\_exc\_raise(my\_err\_new(eno));
> 
> Took me a minute to find that... it's in intern.h, can I call those  
> functions, I mean, should I?

It is OK to call those... I asked about this once:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/90163](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/90163)  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/1807](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/1807)

> Looking through that code, it seems my only option is to create the  
> object myself, then call this internal function.
> 
> Looks like it will work fine, but is this really recommended?

It is fine. You also need to create your error class:  
class MyErr \< StandardError; end # or in C:  
eMyError = rb\_define\_class("MyError", rb\_eStandardError);

Then you can define an accessor in Ruby or in C:  
attr\_reader :eno  
rb\_define\_attr(eMyError, "eno", 1, 0);

I wouldn't worry about writing an init function if your only going to  
be raising these errors from C.

You can instantiate/raise the errors as I described in the earlier  
post.

-Charlie

> Daniel wrote:  
> \> Doesn't rb\_sys\_fail(0) work for this scenario? I thought if a C  
> \> function failed, you could call rb\_sys\_fail(0) and it would raise

the

> \> appropriate Errno. Or am I confused?
> 
> I didn't know about this function, but no, its not for me.
> 
> It only makes sense if the C function you are calling is returning a  
> system error number, i.e. from \<errno.h\>.
> 
> For example, when a C function in our library returns 9, it means

"index

> out of bounds", but when a system call returns 9 it means bad file  
> descriptor, and when the Crypoki API (random example) returns 9 it

means

> CKR\_NEED\_TO\_CREATE\_THREADS.
> 
> I wouldn't want to return Errno::EBADF from my wrapper when the error

is

> index out of bounds!
> 
> Charlie wrote:  
> \> Yeah that works too, if your using the standard library functions.

I

> \> thought from the OP that the error numbers he was working with were

not

> **···**
>
> > \> related to those used by the std lib, but I may be wrong.
> > 
> > Nope, bang on.
> > 
> > Sam
> > 
> > --  
> > [http://www.certicom.com](http://www.certicom.com)

---

<div class="post-metadata">

**Author:** ![Sam\_Roberts](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sam\_Roberts](https://rubytalk.org/u/Sam_Roberts)\
**Post date:** [19 May 2005 15:16 UTC](https://rubytalk.org/t/howto-make-something-like-errno-for-my-c-ext-or-do-i-need-to/18493/5 "2005-05-19T15:16:08Z")

</div>

Wrote Charles Mills \<cmills@freeshell.org\>, on Thu, May 19, 2005 at 07:30:13AM +0900:

> Sam Roberts wrote:  
> \> Looks like it will work fine, but is this really recommended?
> 
> It is fine. You also need to create your error class:  
> class MyErr \< StandardError; end # or in C:  
> eMyError = rb\_define\_class("MyError", rb\_eStandardError);
> 
> Then you can define an accessor in Ruby or in C:  
> attr\_reader :eno  
> rb\_define\_attr(eMyError, "eno", 1, 0);
> 
> I wouldn't worry about writing an init function if your only going to  
> be raising these errors from C.

Worked like a charm, thanks a bunch.

Sam

> **···**
>
> --
> 
> > **[BlackBerry Certicom Official Home Page - Protecting content, applications and...](https://blackberry.certicom.com/en)**
> >
> > With over 35 years of experience in applied cryptography, Certicom provides PKI and key management solutions to help secure the automotive, semiconductors and IoT industry.
