ZF2: how to catch exceptions during rendering - rendering

When rendering starts, my controller code is already ran. If there is an error in the view script, it usually results in an empty or half-rendered page. I have checked the code and there is no event to subscribe to, PHPRenderer just re-throws the exception:
try {
ob_start();
include $this->__file;
$this->__content = ob_get_clean();
} catch (\Exception $ex) {
ob_end_clean();
throw $ex;
}
For controllers, there is a 'dispatch.error' event, but that does not work here. Is there any way to catch these rendering errors and log/display the error properly?

There is a 'render.error' event that you can attach listeners to. See http://framework.zend.com/manual/2.1/en/modules/zend.mvc.mvc-event.html .

There is no such thing in the 2.0.x branch.
For the 2.1.x branch look the other answer.
Anyway, there is no such an event you could use. But I don't think you need it. You should not be having errors in your view, since view is for displaying things only, not for business logic. So I would say you need to fix your view instead of finding a way to catch those exceptions.
As for error logs, you could check apache logs.

Related

How to implement global error handling in Vue

I want to have global error handling in Vue.JS, like the error handling system in Angular 2+. I have tried so much but I could not find a good approach to implement this handling.
Imagine you have many service methods and that these methods should run one after the other (I mean inside each other) so writing then and catch method inside the prevoius service is so ugly and unclean and now I'm looking for clean way to implement such way. I hope you understand what I mean.
As #Badgy mentioned you can install a Vue error handler to catch errors Vue encounters. This can be done as follows:
Vue.config.errorHandler = function (err, vm, info) {
// handle error
// `info` is a Vue-specific error info, e.g. which lifecycle hook
// the error was found in. Only available in 2.2.0+
}
The above code can be located anywhere you like in your javascript. I locate the code just before I create my vue instance. i.e before my var app = new Vue({...}); code. Because it's a global vue error handler it will handle errors from all instances of vue as well as vue components. I find that in practice it mostly catches errors that occur in the vue render methods.
You can read more about it in the official docs here: https://v2.vuejs.org/v2/api/#errorHandler
For more general (non vue related) javascript errors you still need a global error handler like so:
window.onerror = function (msg, url, line, col, error) {
//code to handle or report error goes here
}
Again, this code can be placed anywhere javascript is allowed but typically you will want to place it to run early in your javascript stack. You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
And finally, to catch a "Promise rejection" (i.e. an exception throw from a Promise function) we need to listen for unhandledrejection events since a Promise rejection is not caught by the window.onerror mechanism (thanks to #Blauhirn for the tip). In some browsers (Chrome and Edge currently) Promise rejections can be caught with the following approach:
window.addEventListener('unhandledrejection', function(event) {
//handle error here
//event.promise contains the promise object
//event.reason contains the reason for the rejection
});
For more info see this StackOverflow question: Catch all unhandled javascript promise rejections
I hope i understood your Question Right, but this could be what you are Looking for.
errorCaptured
Type: (err: Error, vm: Component, info: string) => ?boolean
Details: Called when an error from any descendent component is
captured. The hook receives three arguments: the error, the component
instance that triggered the error, and a string containing information
on where the error was captured. The hook can return false to stop the
error from propagating further.
Here is more in-Depth Info About it.
Be careful its Vue 2.5.0+

"An exception has been encountered. This may be caused by an extension

"An exception has been encountered. This may be caused by an extension. C:\Users\mine\AppData\Roaming\Microsoft\VisualStudio\15.0_5f2147be\ActivityLog.xml"
This dialog box is shown when I am trying to build an API in Visual studio 2017. I have disabled all the extensions but still this dialog box is appearing. How I can solve this?
Please learning debugging in order to identify the problems and use try catch of get details of the exception. Then you are able to identify the problems yourself.
Please read this article to learn more about debugging
http://www.c-sharpcorner.com/article/debugging-with-visual-studio/
In order to catch exceptions use the following code block.
try
{
//your code
}
catch(Exception ex)
{
throw ex;
}

How to show error pages in MVC if error occurred in different project

My MVC application uses error pages, configured in the web.config file. All errors display a single web page, regardless of the type of error. This works fine.
The problem I have though, is the exception isn't thrown in the web project, but in my BLL project. So, consider this
//My Web.Ui
public ActionResult Thing()
{
Bll.Statics.DoThis();
return View();
}
and my BLL is
public static void DoThis()
{
throw new Exception("Kaboom");
}
The problem is, when the error occurs here, I don't see my 404 page, I get a standard 'browser' error page explaining something when wrong.
Do I simply just need to wrap all calls in Try Catch from my Controllers so I can throw (re-throw) the exception from the controller itself?
Yes, wrap all your calls in a try-catch block. On the other hand think about the exceptions that you might be catching. Do you want to catch Exception or you want to give different information, like in case of SqlException when you don't have connection to the database, etc. If all you want is to show your custom 404, then you might go with a generic option.

WinJS app with an uncatchable crash

I have random crashes in my WinJS application when navigating between pages.
The problem is that these crashes never occurs when the app is attached to the Visual Studio debugger; so I can't find where they come from.
I use the WinJS.Application.onerror event to prevent crashes, and log what happens, but as this works well when I try with a random exception, my "uncatchable" crashes doesn't seem to fire this event (I don't have anything logged).
Do you have any idea of what could cause these crashes, or any solution to find more informations ?
Sometimes errors can't fire the WinJS.Application.onerror for several reasons (in my app, the problem was in an iframe, in a page not using winjs).
When it happens, errors can be found in the event log, under "administrative events"
Found this on this link :
http://www.davepaquette.com/archive/2012/09/15/windows-8-store-app-crash-logs.aspx
Jason gives a good solution to this problem in this video (start at time 14:48). In his example, the app was crashing if you had a callback and navigated to a different page before the callback completed. Could this be the case for your app? Any more details on what is going on when you navigate?
For others (since it seems you already know about this!):
To be able to debug easier, use the WinJS.Application.OnError event. Wire up an event handler that dumps out information about the problem before the app crashes.
WinJS.Application.onerror = function (info) {
var err = {
errorMessage: info.detail.errorMessage,
errorUrl: info.detail.errorUrl,
errorLine: info.detail.errorLine,
errorCharacter: info.detail.errorCharacter,
};
Windows.Storage.ApplicationData.current.localFolder
.createFileAsync("crash.txt", Windows.Storage.CreationCollisionOption.openIfExists)
.then(function (file) {
Windows.Storage.FileIO.appendLinesAsync(file, [JSON.stringify(err)]);
});
};
The final stop for exceptions in JavaScript is actually window.onerror; not every exception will get thrown through WinJS.Application.onerror. Try hooking window.onerror directly.

How do I catch 'Uncaught typeError: Cannot read property 'isModel' of undefined' during App load?

I'm developing ST2 on Chrome which uses localstorage. From time to time, it appears that the storage schema gets corrupted. When this happens, my app fails during load with "Uncaught typeError: Cannot read property 'isModel' of undefined". The only way to get my app running is to do localstorage.clear() in the console.
I'm assuming this is an ST2 bug, since the occurrence of the corruption seems unrelated to my code, or what I do in my app.
My main question is how do I catch the exception in such a way that either the app can recover, or the user is alerted as to what is going on? Right now, the app simply fails to load. I've tried wrapping the Ext.application() in app.js, but that is executing correctly. My launch function is not being reached.
There must be some weird thing going on with your app. Probably you have found the reason meanwhile and recognised the failing bit is somewhere in your app. Anyway if you are experiencing anything like this then probably the best bet is to catch the error event and do your error handling there. I.e.
window.addEventListener("error", function(errorEvent) {
//
// Add your error handling here
//
// if returns true, the error is suppressed and not thrown
return false;
});