Displaying Xtend source line numbers in stack traces - xtend

Because Xtend code is compiled to Java, the stack trace contains the line numbers of the generated Java source, not the original Xtend source:
package test
class Main
{
def static void main(String[] args)
{
method // line #7
}
def static method()
{
throw new RuntimeException // Line #12
}
}
The stack trace:
Exception in thread "main" java.lang.RuntimeException
at test.Main.method(Main.java:10)
at test.Main.main(Main.java:6)
My question: is it somehow possible to display the Xtend line numbers in the stack trace?
I ask it because it is not too comfortable to browse the Java code first, followed by figuring out the corresponding Xtend code (especially without an IDE).
Besides (in theory) the generated Java code may not be checked in to an SCM system.

Unfortunately, there is no way to do it currently without IDE. If you use Eclipse, you can copy/paste stack trace into Java Stack Trace Console (dropdown next to open consol button in console view) and then, when you click on any part of stacktrace, it will take you to proper place in xtend source - no need to manually backtrace it from generated java source.

Related

Actual exception is not getting logged in MVC when host in IIS

I am running one MVC application where i found one exception in specific method. I will provide here complete details about it.
I am loading some third party grid control from view. to load it i used below code:
#{Html.RenderAction("MasterGridAction", "MyController");}
Now when i access this report from development then this view load and it hits this action method where i used some piece of code there it is throwing error, To catch the error i used try catch block in the method where in catch section i used below code to throw the actual exception like below:
catch (Exception ex)
{
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw(); throw;
}
When exception get catched it sends this to the Application_Error method in global.asax page where i used below code to find out the actual exception like below:
void Application_Error(object sender, EventArgs e)
{
HttpServerUtility server = HttpContext.Current.Server;
if (server.GetLastError() != null)
{
Exception exception = server.GetLastError();
if (exception.GetBaseException() != null)
{
exception = exception.GetBaseException();
ExceptionType(server, exception);
}
else
{
ExceptionType(server, exception);
}
}
}
In this ExceptionType method i get the exception stack and log to the file using "Log4Net". After logging to the file i could see the exception in notepad like below:
Object reference not set to an instance of an object.
Source File: /MyController/MasterGridAction
Stack Trace:
at 3D.Controllers.MyController.MasterGridAction() in D:\MyUser\3D_MVC\Application\3D_OnlyRelease\3D\Controllers\MyController.cs:line 405
--- End of stack trace from previous location where exception was thrown ---
As we can see in above exception it is clearly shown the line number also where this exception get caught.
But once i host this application in IIS server and access the same page then i could see the logged file
There it shows exception like below:
Exception Message: Object reference not set to an instance of an object.
Stack Trace: at 3D.Controllers.MyController.MasterGridAction()
--- End of stack trace from previous location where exception was thrown ---
As you can see the logged exception of IIS server is not having much information when compare to other exception details.
Am i missing anything there to get the complete exception when host in IIS?
Please suggest.
As you can see the logged exception of IIS server is not having much information when compare to other exception details.
This could be because you don't include the PDB files in production.
It might not be a great idea to include them, since that way one can reverse engineer you app with more precision if they hack your server, but it is up to you to decide. I have seen organizations that include them in their production environments.
Try publishing and deploying on a test environment with and without the PDB files to verify this.

Razor The type or namespace could not be found

I have a very, very similar problem to this one.
Hi.
I have an application (Service) targeting .NET 4.61 and trying to move to AspNetCore self host with WebListener.
I am trying to investigate the requirements by testing it out in a console application.
The project i crated is a normal Windows console application, i referenced all the required packages with nuget and created required Startup class and create and start the host with WebHostBuilder.
(I don't have a normal Windows console application, but a dotnet console application on my ubuntu laptop)
We've differed a little bit in what we've tried and tested before hand.
Up until now I just made a HomeController and had Index return a string with "Hello world". Ran the application, went to localhost and got the response. Perfect.
Now all I've added was that Index return an IActionResult by returning View(). Then I made an Index.cshtml file in /Views/Home/ and now I get this:
Stack trace:
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
An unhandled exception has occurred: One or more compilation failures occurred:
followed by 150 lines like this:
gee1fd0w.wuj(4,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
gee1fd0w.wuj(5,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
gee1fd0w.wuj(6,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
...
gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Object' is not defined or imported
gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Void' is not defined or imported
ending with:
gee1fd0w.wuj(33,36): error CS0161: '_Views_Home_Index_cshtml.ExecuteAsync()': not all code paths return a value
at Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()
at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String relativePath, String normalizedPath, Func'2 compile)
--- End of stack trace from previous location where exception was thrown ---
Followed by this:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.GetOrAdd(String relativePath, Func'2 compile)
at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
at Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
Followed by a series of shorter stack traces:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()
End of stack trace
Now I'm not sure that I fully understand the answer the previous poster got:
Mvc's runtime compilation relies on DependencyContext (deps file) to look up compilation references which is only produced when you compile using dotnet CLI. You would have to add compilation references as part of the application startup for view compilation to work.
I did compile through dotnet cli, but is there something else that I'm missing here? When I try to replicate my project by running dotnet new mvc and deleting everything but the things that are in my console-project. It runs right as rain and I get hello world from my Index.cshtml.
Instinct tells me that the problem lies in some file-difference, but I've diffed all of the files and I can't find any difference but app-name. I could have missed something but I'm wondering if it could perhaps be something automagical that I'm not aware of?
EDIT:
Have found the issue, looks like it's a difference in their generated bin/debug/netcoreapp1.1/<appname>.deps.json. Quite a few differences, going to look into it now. But if I copy paste that file from the mvc-project to the console app, everything runs ok.
Unfortunately the console applications deps.json-file is 1300+ lines long and the mvc applications version is 4300+ lines long so I'm not entirely sure it will be feasible to go through it and identifying why one works and the other don't.
EDIT:
It mostly seems to be a whole lot of dependency scopes that differ, a whole lot of the mvc ones contain dependencies to "NETStandard.Library": "1.6.1", but many others as well. There's also a bunch of compile-scopes that the console-application don't have.
In fact this seems to be the only difference. The mvc-application's deps.json-file seem to have ~3000 more lines of dependencies and compile-scopes. So I think I'll give up the idea of figuring out what's causing the problem and and my plan to learn how to "build a console app into an mvc-app" :)
I'll leave the question here in case anyone find themselves in a similar situation.
I have had this happen to me before.
Do a dotnet restore and delete your obj and bin directories.
I may not be technically correct but I think what's happening here is your old build is creating conflicts with your new build that has new (or different) references.
I had the same problem when I moved some of my classes to subdirectory and changed their namespaces. In my case deleting obj and bin directories, as well as running dotnet restore wasn't enough.
I had to change all the references to the old folder in all cshtml files(_ViewImports.cshtml for example) if you used full path as reference to models. So from using ProjectName.Models to using ProjectName.Models.NewDirectory and so on. And then deleting obj bin and running the dotnet command.
This should be fixed by using dotnet restore.
EDIT: Never mind, I see a comment already answered it.
I had this issue with a (ASP).NET Core project. The root cause was because the .csproj file was automagically updated after I mistakenly deleted the backing class of my Razor page (the .cshtml.cs file I deleted). The .csproj file captured an update that explicitly kept the PageModel class out of the build hence the error.
Background info:
I mistakenly deleted a PageModel file (.cshtml.cs) but quickly undid the delete via my Git repo. I did not realize the .csproj file was also updated.

Can Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPage be extended?

In .NET Core you can easily add a detailed exception page for developers:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
This then gives you the exception details:
However, I'd like to include additional exception detail - for instance the SQL query attempted when the exception is a SqlException.
I can write my own exception handler from scratch with app.UseExceptionHandler, but I'd rather extend the built-in error. For instance by adding a tab to the results including additional info.
Is it possible to extend the information on this exception page?

how to resolve stringIndexOutofBoundsException when bitmap is initialized

I am learning to develop a game using libgdix.I follow the book Learning LibGDX game development,second edition.
I am getting stuck on using Bitmapfont.
public class AssetsFonts
{
public final BitmapFont defaultSmall;
public final BitmapFont defaultNormal;
public final BitmapFont defaultBig;
public AssetsFonts()
{
defaultSmall = new BitmapFont(Gdx.files.internal("images/arial-15.fnt"),true);
defaultNormal = new BitmapFont(Gdx.files.internal("images/arial-15.fnt"),true);
defaultBig = new BitmapFont(Gdx.files.internal("images/arial-15.fnt"),true);
defaultSmall.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
defaultNormal.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
defaultBig.getRegion().getTexture().setFilter(TextureFilter.Linear,TextureFilter.Linear);
}
}
when i run this am getting the following error
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: images/arial-15.fnt
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:650)
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:465)
at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:115)
at com.packtpub.libgdx.canyonbunny.game.Assets$AssetsFonts.<init>(Assets.java:125)
at com.packtpub.libgdx.canyonbunny.game.Assets.init(Assets.java:49)
at com.packtpub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:22)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -7
at java.lang.String.substring(String.java:1918)
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:476)
... 7 more
The exception you've got is this one. Practically saying that something went wrong while loading the file. The inner exception shows what went wrong while loading:
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -7
at java.lang.String.substring(String.java:1918)
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:476)
Since there's no call to String#substring at line 476, this is an indication that you're using an older version of libgdx (which is to be expected, because a lot has changed to that file recently). So I'd suggest that you update to the latest nightly (or check the github history at the time of the version you're using), so you know which call is actually causing the error.
Either way, the fact that a substring call failed while loading your file is a good indication that the file is likely to be corrupt or doesnt meet the expected format in another way. Assuming you want to use the font that the libgdx tests uses, then you can download the correct version here and the required image here. Make sure to place the image in the same folder as the fnt file.
Your real problem is that it can't find the font file...
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: images/arial-15.fnt
Make sure the file is in the correct place, which is probably android/assets/images but depends on your project setup.
Also ensure that your run the desktop version with android/assets as the working folder.

swing uncaughtexceptionhandler

I am trying to build a general exception handler for a swing application as described here: http://www.javaspecialists.eu/archive/Issue081.html
I work in jython (python syntax getting compiled to java and executed). My code looks roughly like this (updated):
def launcher(func):
class launcherThread(Runnable):
def __init__(self):
super(launcherThread, self).__init__()
def run(self):
func()
#trying to get the name which can be used to instantiate this in java
cls = ExceptionGroup().getClass()
fullName = cls.__module__ + '.' + cls.__name__
System.setProperty("sun.awt.exception.handler", fullName)
Thread(ExceptionGroup(), launcherThread(), 'Cross ExceptionHandlerThread').start()
class ExceptionGroup(ThreadGroup):
def __init__(self):
super(ExceptionGroup, self).__init__("HardenedGroup")
def uncaughtException(self, thread, exception):
#make a fancy dialog displaying str(exception)
If I test it it works fine however in the production enviornment it failes.
For testing I launch my program in Eclipse (PyDev), the production enviornment is a third party application written in Java, that has a Jython console build in. The application supports adding of custom menu entries, and putting jython scripts on these.
The main difference I see between testing and production enviornment is that in the production enviornment the swing threads are allready started (the third party application utilitizes swing). Does this cause my ThreadGroup setting to fail, or is there another reason why this is not working?
How can I get the Involved threads (exceptions ar thrown as a result of buttonActions) to check their defaultException handlers? If (as I am afraid) it should turn out that the third party installed its own handler (all exceptions are written to a log file) how can I make a new swing worker thread? (I don't want to catch the exceptions created by the host application after all)
Question recap:
1. How can I check which threads are started for the function func passed into the launcher function and see thier uncaught exception handler?
2. Can I enforce a seperate swing dispatcher for my gui part and the main applications gui part? (If I exitOnClos on a frame of my add in, the third party application closes)?
Update:
Considering the anwser from lbalazscs I am trying to use the sun.awt.exception.handler property, but it has no effect, the exceptions still end up in the log file (applications dfeault behaviour). Am I using it right? (p.s.: I am on Java 1.6)
If you have Java 5 or higher, you can also use Thread.setDefaultUncaughtExceptionHandler(), which is also described in a newer "Java Specialists' Newsletter":
http://www.javaspecialists.eu/archive/Issue089.html
And here is the newest Java 7 version:
http://www.javaspecialists.eu/archive/Issue196.html
Also see this:
Why bother with setting the "sun.awt.exception.handler" property?
EDIT: This is how I use Thread.setDefaultUncaughtExceptionHandler (in Java...):
public static void setupGlobalExceptionHandling() {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
handleException(e);
}
});
}