Determine route that causes PayloadTooLargeError - node.js

We have a NodeJS app that uses Express. We've seen a few PayloadTooLargeError message start to pop up, but the problem is we don't know whats causing it or how to recreate. The error message also doesn't give us any indication of which route the error is occurring on. Is there a way for the stack trace or error message to detail which route is causing the error? We have quite a few routes with APIs from multiple services, so debugging them all is almost impossible.

Adding a custom middleware that logs the request uri before the route is called will allow you to see which uris are throwing errors.

Related

Await in async function without try and catch in sails mongo db

I am using sails JS and mongo db for my web app.
my code is working fine. But i am having a doubt.
my database statements are like this:
var a = await users.create({username:username,id:id}).fetch();
No try and catch. No error handling.
it is working perfectly fine in my local host. If i Moved to production, will that create a problem??
Any promise that has any chance of rejecting MUST have an error handler of some type. If you don't and you hit that error, your JS will essentially crash with an unhandled exception.
It's just bad programming not to handle any possible errors in some way. You should catch the error, log it and return some sort of error status from whatever http request probably initiated this call.
No error handling. it is working perfectly fine in my local host.
Sure, as long as there's NEVER an error with the database call, it works perfectly fine. What would you expect to happen if there was some sort of database error? Do you want your server crashing? Would you know what was causing it if your server just crashed? Would your users get any feedback that there's a problem?
If i Moved to production, will that create a problem??
It certainly could. If something happened with the data for this call or something happened to your database that led to an error, then your server may crash and whatever http request initiated this would never get a response.
It is just poor programming to not handle all possible errors in some way in your server. It's a common shortcut or lack of attention to detail, but it's always a mistake. As a senior developer involved in code reviews for less senior developers, this was a reason to fail the code review and require further attention to proper error handling.
No it will Not cause probleme but if you got error when this procedure is runing how would you know imagine you did that in many several places in your app and there is some ood issue happining how would you know where is coming from ?
so you Need to do that for more error handling!

report error whenever an error occur in project

My requirement is to send the error to developers whenever an error occurs in the project (ex: unable to fetch the user from the database, var x is not defined, myfun() is not a function etc.)
We are using
NODE for backend language
Express for routing
GCP for deployment
PM2 package for management
Express default error handling is not working because we have many async codes, and it's very difficult to check and modify the project using express next() function.
I didn't find any way to report project error via GCP
and report error via pm2 is not working
Please tell me any third party package to report project errors or any other way of reporting.
User Stackdriver as already advised, and for Stackdriver to be useful, make sure that all errors are logged properly in your code, so that meaningful error messages are displayed in your Stackdriver reports.
Check this post for different methods of catching errors with Javascript, and how to deal with asynchronous calls.

WebApi application on Azure returning 500 errors for some endpoints

I am setting up an ASP.NET WebApi application running on the Azure web application environment. Most of my controllers and endpoints work fine, but there are about 10 routes that return 500 errors "An error has occured". It's not random. It's the same routes every time and I can find no pattern (not all the HTTP methods, from different controllers where other routes in that same controller work fine, and so forth)
When these errors occur, no error logging gets triggered as far as I can tell in the app. (I am using Raygun.IO if that matters). I tried adding a global.asax file with following lines in the Application_Error function:
Exception ex = Server.GetLastError();
new RaygunClient().SendInBackground(null, ex);
but as far as I can see, it doesn't get triggered when these 500 errors occur. The only thing I have found in the Azure server logs is the following warning in the Failed Requests log:
However, I don't see any errors in the trace previous to that point. I'm also not finding any other errors in any of my Azure logs that I can relate to these failing routes.
It's .NET 4.6 (tried 4.5.1 and 4.5.2 with no difference) WebApi using an OWIN startup class. Also tried updating all my packages to no effect.
Check to be sure you don't have routes that conflict. When a URI matches two or more controller actions, and thus Web API can't pick one, you will get a 500 error.
If you have a consistent repro, then you can use remote debugging to attach to your web app and debug it (https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-troubleshoot-visual-studio/). If you can repro it through the browser, you can also turn the custom errors page off so that it shows the stack trace through the browser (<customErrors mode="off"/> under <server.web>).
Also, you can enable better diagnostic logs using the web app settings. Information on how to do so is here: https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/ under the section "Enabling diagnostic logs".
Also, as Brent said, URL conflicts can cause some nasty errors which aren't immediately obvious.
I figured out what was going on. There were actual errors occurring (mostly SQL related), but the way that we had configured the functions and error logging in those functions, the actual errors were being swallowed up and lost, overriding the error logging behavior of Raygun. I went through and ripped out the Try/Catch blocks we were using for error trapping and logging and the real errors finally emerged into the Raygun dashboard.

How to proper log every exception using OWIN

my question should be quite simple, but unfortunately I had no luck in solving it.
Basically, I have some Web API controllers hosted by OWIN and deployed on Azure.
I really need to track down exceptions that occur in each middleware (for example OAuthAuthorizationServerProvider or SignalR Persistent Connections), but I definitely don't have a clue on how to achieve it.
I tried Elmah, but it doesn't seem to work properly with OWIN due to lacking HttpContext.
I tried using log4net, but I'm only able to log exceptions thrown by Web API Controllers using a custom ExceptionFilterAttribute.. others are ignored.
I tried to define a custom LoggerFactory and to assign it in Startup, using app.SetLoggerFactory(new MyLoggerFactory()),
but exception thrown by other middlewares are not logged.
I tried to get at least a meaningful error message sent to the client, but despite <customErrors mode="Off"/> and <deployment retail="false"/>, Azure refuses to return anything but {"message":"an error has occurred"}.. I tried both Azure Web Sites and Azure Cloud Services.
I saw some cloud alternatives that should work with OWIN, like Elmah.io or Raygun.io, but I don't need their cloud features and it is definitely not worth paying hundreds $ per year just to log some exceptions.
What should be the best way to log any possible exception thrown by my application?
Thanks for your help
have you take a look at this link ? http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling
Because you can't catch all the exceptions using an exceptionFilter, they propose to use a IExceptionLogger and IExceptionHandler to allow global error handling in Web Api 2.
After that, if it's not fit your need, you can construct an OwinMiddleWare that you will place in first position (before the Authenticate stage), this middleware could :
create a requestId in the header of the response
analyse the response code, before sending response, and if it's not a IsSuccessStatusCode, you could log the exception message to a DB and replace the content of the response to send a simple error message to the client using the requestId (to allow you to find the related exception in your db)
hope this help

Azure failed request error details

I've got an Azure app up and running, but various requests generate a 500 error. There are no other details that come back from the server to let me know exactly what the problem is. No stack trace, no error message. The only thing I get back from the server are the http headers indicating I've got an error.
I've done a little looking around but can't seem to find a way to retrieve the error details that I'm looking for. I've seen some articles that suggest that I enable logging, but I'm not sure 1) how to do that, 2) where those log files would go and 3) how to access said log files. I've seen posts that say to add a whole bunch of code to my application to enable logging, but all I'm looking for is an error message and a stack trace from a 500 error. Do I really have to add a bunch of code to my app to see that information? If not, how can I get at it?
Thanks!
Chris
The best long-term solution is to enable Azure Diagnostics, which I think is what you're referring to. If you want a quick-and-dirty solution, you can log errors out to a file and then RDP into the role instances to view them. This is very similar to what you would do on a server in your own datacenter.
You can create the logs however you like. I've used log4net and RollingFileAppenders with some success. Setting the logfile path to something like "\logs\mylog.txt" will place the logs in the E: drive of the VM. Note you'll still need code somewhere in your app to capture the error and write it to the log - typically the global error handler in Global.asax is a good place for that.
You'll also have to enable RDP access to your role instances. There are many articles detailing how to do that. Here's one.
This is not a generally recommended approach because the logs may disappears when the role recycles or is recreated. It's also a pain in the butt to log to keep an eye on all those different servers.
One other warning - it's possible that the 500 error is due to some failure in your web.config. If that is the case, all the the application-level error logging in the world isn't going to help you. So be sure that your web.config is valid, and also check the Windows Event Logs while you're RDP'd into the server.
500 internal server error is most generally caused by some problem on the server when it was not able to understand incoming requests or there was some problem in configuration. So, try to run the app locally and see if there is some problem. You can record errors in a database in catches/application_error and also can use tracing. Believe me they are very helpful and worth a few extra lines of code.
For tracing have a look here, http://msdn.microsoft.com/en-us/magazine/ff714589.aspx

Resources