Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Environment.RaiseError<T>(...) #82

Open
axefrog opened this issue Jul 16, 2011 · 8 comments
Open

How to use Environment.RaiseError<T>(...) #82

axefrog opened this issue Jul 16, 2011 · 8 comments

Comments

@axefrog
Copy link

axefrog commented Jul 16, 2011

In my custom JavaScript object, I'd like to throw JavaScript errors when one or more of the arguments supplied to the constructor or to one of the member functions is invalid.

As best I can tell, I should use environment.RaiseError<T>(prototype, message).

What is the correct way to use this method and/or its overloads? I'm catching the UserError exception that is subsequently thrown, but I don't know if I'm doing it correctly because I don't know what the type argument T refers to, or what the correct way to access my error message in the thrown exception is.

@otac0n
Copy link
Collaborator

otac0n commented Jul 16, 2011

The raise error was made generic to fulfil F#'s requirements for the function callsites to have a certain return value.

Effectively the way it is used is like this:

public MyReturnType MyFunction()
{
    return RaiseUserError<MyReturnType>("message")
}

@axefrog
Copy link
Author

axefrog commented Jul 17, 2011

In the wiki article, the methods are static, so there is no inherited RaiseUserError method that is available to call. There are variations on this in the Env property of FunctionObject and CommonObject, but all of them take a first argument of type CommonObject and the second argument for the error message. None of them seem to match the RaiseUserError method in your example.

@axefrog
Copy link
Author

axefrog commented Jul 17, 2011

I apologise if you simply meant RaiseUserError where User is meant to be replaced by the appropriate method depending on the error type. It would be handy to get an understanding of which error type to use at what time. For example I want to throw an error if I was expecting an argument of type double but received NaN instead (i.e. no argument passed). Is that a range error?

@axefrog
Copy link
Author

axefrog commented Jul 17, 2011

One more thing - if you can help me properly understand the correct way to throw errors, even when exposing a CLR method as a JavaScript global function (hence it won't have access to the Environment object), I'll write it up in the wiki for you.

@otac0n
Copy link
Collaborator

otac0n commented Jul 17, 2011

Methods can have access to the global Environment method, if they need it. You just need to use this as your method signature:

public TReturn MyMethod(Environment env, CommonObject @this, TArg myArgs, ...)

@fholm
Copy link
Owner

fholm commented Jul 17, 2011

@otac0n: Unless something has been changed recently the first and second parameters should be FunctionObject and CommonObject, not Environment and CommonObject:

public BoxedValue MyMethod(FunctionObject self, CommonObject @this, BoxedValue arg1)

@fholm
Copy link
Owner

fholm commented Jul 17, 2011

You can also access the Environment from both self and @this parameters.

@axefrog
Copy link
Author

axefrog commented Jul 17, 2011

What about the RaiseError<T>(prototype, message) method? Is that not designed for external use? I ask because when used, the thrown exception doesn't include the specified message as part of the exception's Message property, rather the message returns [object Error]. To get the message, it looks like I have to call ((IronJS.ErrorObject)ex.Value.ClrBoxed).Members["message"], which seems a bit obtuse and probably not the right way to go about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants