Is there an ActionScript 3 error 1046 handler? - actionscript-3

I'm talking about this error:
ERROR 1046: Type was not found or was not a compile-time constant: CustomType
Where CustomType is defined in a class in an .as file. I know you need to import that file in order to avoid this error, but my question is, is there any error handler for this error?
I mean, is there a way to say, for example:
onErrorHandler {
trace("You need to import the class!");
} else {
private var myObject:CustomType;
}

That error is a thrown at compile time, so no.
The import statement tells Flash what classes need to be compiled for your current class to function as expected. You can't write code to catch an error that is thrown at compile-time, because it doesn't compile and thus doesn't get executed.
It is the compiler's responsibility to catch these errors, not yours.

Related

The noticeError method was not found. while using Coldbox and NewRelic for Error tracking

I'm just using NewRelic error trapping for my coldbox application. From OnException method, I'm just sending the error struct to log the error.
My code in onexception method
public function onException(event,rc,prc){
NewRelic.logError( prc.exception.getExceptionStruct());
}
The logerror() method resides in NewRelic.cfc and contains the following code
public boolean function logError(
required struct exception
) {
var cause = arguments.exception;
var params = {
error_id = createUUID(),
type: arguments.exception.type,
message: arguments.exception.message
};
writeDump(this.newRelic);
this.newRelic.noticeError(cause, params);abort;
return true;
}
So while error, I'm gettig the following error.
The noticeError method was not found.
You can see that, the noticeError() method is there in the object, but it is overloaded with arguments.
I'm using the same code for NewRelic error trapping in another coldfusion project without any frameworks.
Calling error.cfm through Cferror tag, and the code in error.cfm as follows
<cfset Application.newRelic.logError( variables.error )>
And in NewRelic.cfc, the logerror() method contains the same code as in the coldbox application. But it is logging errors in NewRelic without any issues.
This is the method I need to notice errors and log it in NewRelic.
noticeError(java.lang.Throwable, java.util.Map)
So I just thought to get the classname of the first argument Cause through the following code from both applications within logError() in NewRelic.cfc, to get the difference.
writeDump(cause.getClass().getName());
I'm getting
coldfusion.runtime.ExceptionScope for Coldbox application
and
coldfusion.runtime.UndefinedVariableException for normal coldfusion application
The cause argument is not throwable from coldbox application. So how to get the original error struct from coldbox application? and make it throwable to fix the noticeError method was not found issue.
The change in the underlying class happens when ColdBox duplicates the error object with CFML's duplicate() method. I doubt that ColdFusion behavior is documented anywhere, but I don't see an easy way to get around it right now other than creating your own instance of a java.langException and populating it with the details of the original error.
If you want to modify the ColdBox core code, this happens here:
https://github.com/ColdBox/coldbox-platform/blob/master/system/web/context/ExceptionBean.cfc#L43
I have entered this ticket for the ColdBox framework for us to review if we can stop duplicating the error object in future versions of the framework.
https://ortussolutions.atlassian.net/browse/COLDBOX-476
Update: Adam Cameron pointed out this ticket in the Adobe bug tracker that details this behavior in the engine. It was closed as "neverFix".
https://bugbase.adobe.com/index.cfm?event=bug&id=3976478

Error: Call to a possibly undefined method startTest

I'm new to flash development. I got an actionscript (3.0) code to work with, which I'm trying to compile using mxmlc.exe
import flash.display.*;
startTest();
function startTest() {
// Create objects to other classes to start display
}
When I compile it with the below command
mxmlc.exe Main.mxml
Where the above .as file is included, I keep getting the following error
Error: Call to a possibly undefined method startTest.
There in no class or package is defined in the .as file.
Please provide some inputs here.

error compiling junit test with expect exception

I am having difficulty using junit 4's expected annotation to look at exceptions. I can't compile the code because there's an unhandled exception.
Here's a simple example that creates the situation:
import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
public class Simple {
#Test(expected=UnsupportedEncodingException.class)
public void simpleTest(){
String a = "";
a.getBytes("UTF-123");
}
}
I get a compilation error saying "Unhandled exception type UnsupportedEncodingException"
This makes sense, and I can fix this by declaring that simpleTest throws UnsupportedEncodingException but I've seen lots of examples online where people don't do that (which would be nice, when writing lots of test cases).
Is there a way to configure the test case so that I don't have to explicitly declare what exceptions will be thrown?
As far as I know, UnsupportedEncodingException is a checked exception. So, compiler would expect a throws clause for a checked exception. I guess your code will work, if say you tried with an unchecked exception like ArithmeticException.

Does addEventListener(ErrorEvent.ERROR, handler) handle all type of error event in actionscript3?

Does addEventListener(ErrorEvent.ERROR, handler) handle all type of error event, for example, IOErrorEvent.IO_ERROR, SecurityErrorEvent.SECURITY_ERROR, and other all error events?
I'm looking for addEventListener() version of try catch(e:Error)(e:Error can catch all type of errors).
You can add error handlers to the UncaughtErrorEvents object:
loaderInfo.uncaughtErrorEvents.addEventListener(
UncaughtErrorEvent.UNCAUGHT_ERROR, errorHandler);
function errorHandler(e:UncaughtErrorEvent):void {
if(event.error is Error) {
// handle error
}
// suppress error dialog
e.preventDefault();
}
This is only possible in Flash Player 10.1 and above.
You can find more information here: flash.events.UncaughtErrorEvents
This can be especially helpful for handling exceptions from a loaded SWF. I assume you have a good reason for doing this?
Each event type is registered as a different String, so the only way to catch all events of varying types is to listen for uncaught errors that get relayed by a special UncaughtErrorEvents dispatcher. Notably, this exists on any DisplayObject's loaderInfo property # DisplayObject.loaderInfo.uncaughtErrorEvents.
Demonstrating 3 ways to receive uncaught errors...
private var loader:Loader = new Loader();
public function MyDocumentClass ()
{
// 1: Listen for all errors in the application:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// 2: Listen for errors from the child swf being loaded:
loader.load(new URLRequest("file.swf"));
loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// 3: Listen for errors from Loader doing the loading:
loader.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// This seems like it would work, but wasn't working in tests I ran:
stage.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
if (event.error is Error)
{
var error:Error = event.error as Error;
// do something with the error
}
else if (event.error is ErrorEvent)
{
var errorEvent:ErrorEvent = event.error as ErrorEvent;
// do something with the error
}
else
{
// a non-Error, non-ErrorEvent type was thrown and uncaught
}
}
From Adobe's documentation...
An UncaughtErrorEvent (extends ErrorEvent) object is dispatched by an
instance of the UncaughtErrorEvents class when an uncaught error
occurs. An uncaught error happens when an error is thrown outside of
any try..catch blocks or when an ErrorEvent object is dispatched with
no registered listeners. The uncaught error event functionality is
often described as a "global error handler."
The UncaughtErrorEvents object can be accessed in two ways...
LoaderInfo.uncaughtErrorEvents -- to detect uncaught errors in code defined in the same SWF.
An object that dispatches an uncaughtError event when an unhandled
error occurs in the SWF that's loaded by this Loader object. An
uncaught error happens when an error is thrown outside of any
try..catch blocks or when an ErrorEvent object is dispatched with no
registered listeners.
Note that a Loader object's uncaughtErrorEvents property dispatches
events that bubble through it, not events that it dispatches directly.
It never dispatches an uncaughtErrorEvent in the target phase. It only
dispatches the event in the capture and bubbling phases. To detect an
uncaught error in the current SWF (the SWF in which the Loader object
is defined) use the LoaderInfo.uncaughtErrorEvents property instead.
Loader.uncaughtErrorEvents -- to detect uncaught errors in code defined in the SWF loaded by a Loader object.
An object that dispatches an uncaughtError event when an unhandled
error occurs in code in this LoaderInfo object's SWF file. An uncaught
error happens when an error is thrown outside of any try..catch blocks
or when an ErrorEvent object is dispatched with no registered
listeners.
This property is created when the SWF associated with this LoaderInfo
has finished loading. Until then the uncaughtErrorEvents property is
null. In an ActionScript-only project, you can access this property
during or after the execution of the constructor function of the main
class of the SWF file. For a Flex project, the uncaughtErrorEvents
property is available after the applicationComplete event is
dispatched.
Some important details from Adobe's documentation...
When an uncaughtError event happens, even if the event is handled,
execution does not continue in the call stack that caused the error.
If the error is a synchronous error, any code remaining in the
function where the error happened is not executed. Consequently, it is
likely that when an uncaught error event happens, your application is
in an unstable state. Since there can be many causes for an uncaught
error, it is impossible to predict what functionality is available.
For example, your application may be able to execute network
operations or file operations. However, those operations aren't
necessarily available.
When content is running in a debugger version of the runtime, such as
the debugger version of Flash Player or the AIR Debug Launcher (ADL),
an uncaught error dialog appears when an uncaught error happens. For
those runtime versions, the error dialog appears even when a listener
is registered for the uncaughtError event. To prevent the dialog from
appearing in that situation, call the UncaughtErrorEvent object's
preventDefault() method.
if you want to catch all the errors in your application you should definitely use try-catch blocks. By using addEventListener you are adding listener to a specific object and you will catch the errors only there.

Global Error Handler for Flash Player 10.1 not working

Trying to implement the new FP 10.1 Global error handler into my projects but no matter what I do any uncaught error will still show up the Exception window (both in debug and release versions of the SWF). All I want to do is to prevent these popups but instead send a message to my logger. Here's my code ...
EDIT: I simplified the code now. Could somebody do me a favor and test the following class and see if it's works for him? Because it's doesn't for me! ...
package
{
import flash.display.Sprite;
import flash.events.UncaughtErrorEvent;
public class GlobalErrorHandlerTest extends Sprite
{
public function GlobalErrorHandlerTest()
{
stage.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
throw new Error();
}
private function onUncaughtError(e:UncaughtErrorEvent):void
{
var message:String;
if (e.error["message"])
{
message = e.error["message"];
}
else if (e.error["text"])
{
message = e.error["text"];
}
else
{
message = e.error["toString"]();
}
trace("Uncaught Error: " + e.text);
}
}
}
I had the same issue as above - I was referencing stage.loadInfo, believing that as that references the stage, it would capture all uncaught errors. However, that doesn't work, you have to actually follow grapefukt's suggestion verbatim: On the actual base display object, place the code
loaderInfo.uncaughtErrorEvents.add...
When you try to place onto stage.loaderInfo or frame.loaderInfo, it has no effect.
In my case, I had to place it in the class that extended the base display object. Very Odd.
The docs say that:
The UncaughtErrorEvents object that dispatches the event is associated with either a LoaderInfo object or a Loader object.
Thus you must listen to the loaderInfo's uncaughtErrorEvents property of your topmost display object:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
UPDATE: I think I may know why you think this isn't working. I made the mistake of testing inside the debugger. When the debugger stopped on the runtime errors I assumed that this proved my uncaughtErrorHandler function was not working. In fact, this was in error. It's just a quirk of the debugger. The debugger will still stop as if it is an unhandled error or exception, but if you press Run again you'll see it DOES execute the error handling code. See my thread Flex 4.0/4.5 global error handling for more info.
Original response:
I am looking for this same info. None of the examples in the API doc nor various blogs on the subject work for me. I've tried just loaderInfo, stage.loaderInfo, systemManager.loaderInfo... It makes no difference. I even tried all of them in a single test case! The addEventListener are being set but the uncaught errors are not firing the uncaughtErrorHandler. Argh. I have wasted far too much time on this! I've reduced it to a very simple program much like above except with all the code in the main mxml file.
How about this: can someone post a Global Error Handling example that DOES work? I'm using Flex SDK 4.1 (I've also tried with 4.5), targeting FP 10.1 (or 10.2 for 4.5), in a mx:Application-based mxml Flex project.
My thread: Flex 4.0/4.5 global error handling
You must set up the listener not to a specific view, but to the main stage object, as it's at the top of the display list (thus picking up any exception of any of its children).