Why does the validateMonth method of the datejs api throw an exception and return a boolean? - datejs

Why does the validateMonth method of the datejs api throw an exception and return a boolean?
Date.validateMonth(14)
Will throw the following error.
Timestamp: 12/21/2012 10:56:16 AM
Error: RangeError: 14 is not a valid value for months.
Source File: http://localhost:18103/soart/scripts/date.js
Line: 21
To me this doesn't make sense. It should return a boolean without throwing the exception. The purpose of the method is to check for a valid month after all. Am I missing something?

According to the DateJs documentation it's doing exactly what is expected. To counter this, you could wrap the check in a try catch statement.
It might throw the exception so that it can return a meaningful error depending on what's been entered. So if a string is passed you get a different error message. In your case, the actual error message is just
14 is not a valid value for months

Related

How to make Web API return all errors for the validated model?

ModelState returns only 1 error at a time when validation occurs. How to force Web API to return all errors at once.
services.AddControllers()
.ConfigureApiBehaviorOptions(setupAction =>
{
setupAction.InvalidModelStateResponseFactory = context =>
{
// context.ModelState here always contains only 1 error, even if I have more invalid fields in
}
};
Considering your scenario, it can be said that,
In InvalidModelStateResponseFactory there is a constructor callled ModelStateDictionary() under this you would get ErrorCount which can get you number of error.
But if you are expecting you would get all the error details together, you cannot do that. It will always fetch first one and terminates execution and entered to your custom InvalidModelStateResponseFactory middleware.
Note: So when any error encounters it terminates and return rather executing further as a results we always get first one. You
could get our official document here
Hope it would help you to guide you through.

Is it possible to extract only the meaningful informantion from Exception.Message

This is what I get when I use Exception.Message :
System.Web.Services.Protocols.SoapException: The server can not process the request. ---> System.NullReferenceException: Object reference not set to an instance of an object in . in WebService.ProcessRequestArc... --- End of inner exception stack trace ---
Is it possible to configure a web application so that exceptions only send the part in bold in my example?
Or do you know of any way to extract only that part?
The Message property of an exception does not follow any pattern. In particular, the Message property will be different depending on the current culture settings (language).
But even if you are only concerned about a single language, you should never depend on anything in the Message property. It simply contains whatever the developer who threw the exception thinks you might want to see.
In particular, it does not contain anything that you should display to your users!
I didn't quite understand your problem at first. To avoid seeing the Stack Trace, you simply had have display Server.GetLastError().Message. On further examination I see that your problem is that exceptions are getting wrapped into a HttpException, and the Message from HttpException concatenates the method info with the original exception message.
Solution
In web.config you need to add a custom error section.
<customErrors defaultRedirect="Error.aspx"
mode="On" redirectMode="ResponseRewrite">
In Error.aspx you can do the following to check if it's an HttpException, and if so get the inner exception's message.
var ex = Server.GetLastError();
string message;
if (ex != null)
{
message = ((ex is HttpException || ex is SoapException) && ex.InnerException != null)
? ex.InnerException.Message : ex.Message;
}
else
{
message = "An error has occurred.";
}
Response.Write(message);
On your page you just want to show Server.GetLastError().Message.
Here is the before and after for a divide by zero error:
Before: Default.aspx(4): error CS0020: Division by constant zero
After: Attempted to divide by zero.
For SoapException to actually have the InnerException property not be null, then you need to throw a SoapException with the actual exception inside.
Here are some links from MSDN regarding the customErrors section and error handling:
customErrors Element
Complete Example for Error Handlers

Issue with Throw in asp.net

Below, why doesn't Throw maintain my originating line number?
If I run the DerivedPage, my log file lists the error as line 7.
Shouldn't it say line 4? It would make sense if I had Throw ex, but I don't.
Isn't Throw by itself just supposed to rethrow and bubble up the error?
If I remove the Try...Catch block entirely in DerivedPage, then my log file correctly lists 3 as the error line, but I am not able to log any info in case of an error.
What can I do to maintain my DerivedPage, and still have my log keep the correct line number?
Public Class DerivedPage Inherits BasePage
Page_Load(o,e)
Try
Dim a = 3 -"a"
Catch ex As Exception
log.Info(...)
Throw
End Try
End Class
base page:
Public Class BasePage
Protected Overrides Sub OnError(e)
MyBase.OnError(e)
log.Error(Me.GetType(), Server.GetLastError)
End Sub
End Class
EDIT: log.Error does output the InnerException if it exists. It does in this case. However, the stack trace for the InnerException doesn't contain a line number, just the Exception details.
When an exception is rethrown, the original exception details are stored in the InnerException property of the object. You should have all of your details there.
There is a method that you can implement that will give you the correct line number in your stack trace:
Rethrowing exceptions and preserving the full call stack trace - Fabrice's weblog
The code is in C#, but the code for the PreserveStackTrace method should be relatively easy to port over to VB.NET.
SOLUTION: Two solutions per Wrong line number on stack trace are throw a new exception with the current exception as the inner, or use a helper method.
I'm going to go with throwing a new exception.

ASP.NET Error Object reference not set to an instance of an object

Object reference not set to an instance of an object
I am getting this error sometimes.
I have a page where I load questions and for some question system generate this error. Why do I get this error? Please help me.
It means that somewhere something dereferences a null reference. Crude example:
Foobar foo = null;
foo.DoSomething(); // this line will trigger a NullReferenceException
Here's what you can do:
compile in debug mode
run it until it crashes
examine the stack trace; it will tell you where the exception originates (unless you're doing stupid things like catching and rethrowing improperly). You may have to follow the chain of inner exceptions
if that doesn't provide sufficient information, step through your code with a debugger until right before the call that makes it crash. Examine any variables that might be null.
Once you have found the culprit, you need to find out why it is null and what to do about it. The solution can be one of:
fix incorrect program logic that causes something to be null that shouldn't be
check if something is null before dereferencing it
handle the exception at a point that makes sense, and translate it into something more meaningful (probably some sort of custom exception)
You got null reference and if it can't handle it will show that message.
It can caused by some case
The return value of a method is null
A local variable or member field is declared but not initialized
An object in a collection or array is null
An object is not created because of a condition
An object passed by reference to a method is set to null
I suggest you to track the null value using a debug mode
You get this error, whenever you are trying to make use of variable with Null value in it.

Object Reference Error - Show class name with error?

When I get an Object Ref error, it can sometimes be a real pain to find out which variable is causing the error (when you can't debug). Is there a way for this error to throw the classname that isn't assigned?
So: I want the name of the type of the variable that was unexpectedly null.
Thanks in advance.
I dont think you can get the class name, the closes I get is to get the class and method name, then the stack trace:
try
{
}
catch ( Exception ex )
{
xxx.API.ErrorHandler.Handler.HandleError( ex, System.Reflection.MethodBase.GetCurrentMethod().Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName );
}
Well, its only the information in the stack trace which would be the first step in finding out where the error has originated. Also you should make sure you check the complete stack trace (all inner exceptions too). This would give you the method name with complete namespace. So that should be fairly good step to see where the error is, unless the standard coding is really bad.

Resources