Symfony 2 File Upload. How to throw exception on move() - file-upload

Reading the symfony docs, http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does automatically
$this->file->move($this->getUploadRootDir(), $this->path);
But if I did
if ($this->file->move(...))
I got an error
Catchable Fatal Error: Object of class Symfony\Component\HttpFoundation\File\File could not be converted to boolean in .../xxx.php line 96

look at the source code : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/File/File.php#L108
It seems that an exception is already thrown.
Cheers

Related

Could not load file or assembly 32 bit assembly

I have been searching and trying for many hours. I am having 'System.BadImageFormatException' when running my WCF service hosted on IIS 10.0 from 64-bit client application.
- I tried to set Enable 32 bit applications = true .
- I checked that target CPU = Any CPU .
- I double checked on the applicationHost.config file on the <ApplicationPools> section and make sure that Enable 32 bit applications = true .
All these attempts with same result, the same error message:
Could not load file or assembly 'DBConnNET4, Version=1.0.34.0,
Culture=neutral, PublicKeyToken=60d47e8c3d6f39e7' or one of its
dependencies. An attempt was made to load a program with an incorrect
format.
When debugging, I am getting the exception inside class from the referenced library, exactly here:
Public Class BusinessObject
Private Function Invoke()
Dim obj As new BusinessObject()
obj.ctrlQueryRun()
End Function
Private Function ctrlQueryRun() As Short
Try
'*** the exception occurs at this line, GetAllItems() is function in the same class
If Me.GetAllItems() = cERROR Then Return cERROR
'' Some code Here
Catch ex As Exception
Return cERROR
End Try
Return cSUCCESS
End Function
End Class
All the solutions I found on the Internet were telling to do the previous attempts, any other suggestions? Is there any thing I forgot to check? Many thanks in advance for anyone helping or trying to help.

Handling Windows Store App exceptions from GetFileAsync

I have a problem with the following code example:
Windows::Storage::StorageFolder^ location = Package::Current->InstalledLocation;
try
{
task<StorageFile^> GetFileTask(location->GetFileAsync(sn));
GetFileTask.then([=](StorageFile^ file)
{
try
{
task<IBuffer^> ReadFileTask(FileIO::ReadBufferAsync(file));
ReadFileTask.then([=](IBuffer^ readBuffer)
{
// process file contents here
});
}
catch(Platform::Exception^ ex)
{
// Handle error here
}
});
}
catch(Platform::Exception^ ex)
{
// Handle error here
}
When using a filename that doesn't exist the function throws an exception:
Unhandled exception at 0x0FFCC531 (msvcr110d.dll) in GameTest2.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
I've been searching the internet and this exception breaks only when connected to the debugger. I'm using VS 2012. I've turned off all the relevant 'break on exception' but it still causes the debugger to break and non of my handlers are getting a chance to handle the exception.
If the file is missing I would expect the GetFileAsync method to throw a 'File doesn't exist' exception. Not sure why it keeps throwing the 'Invalid parameter' exception.
This is starting to bother me and I just can't find any known solution to this issue. Anyone have any ideas?
I'm going to try and change the method to not use the task<> code. Instead I'll call the GetFileAsync using 'await'. However I believe 'await' will just cause the calling thread to wait until the GetFileAsync has finished, which kind of defeats the point of asynchronous loading.
I'm wondering if this is a common issue with exception handling when using tasks.
Update:
OK, I've now found the solution:
task<StorageFile^>( location->GetFileAsync(sn)).then([](StorageFile^ openedFile)
{
return FileIO::ReadBufferAsync(openedFile);
}).then([](IBuffer^ readBuffer)
{
// Process file
}).then([](task<void> t)
{
try
{
t.get();
}
catch(Platform::Exception^ e)
{
// Handle error
}
});
It seems there needs to be an extra 'then' condition added to the end of the chain to pick up the exception.

Handling exception in Javaparser

I am trying to handle the exception produced by Javaparser library due to token error. I used the following code.
String content=getTheSource();
ByteArrayInputStream bin=new ByteArrayInputStream(content.getBytes());
try
{
CompilationUnit cu=JavaParser.parse(bin);
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//my handling code here
}finally{
bin.close();
}
However, the exception was never caught and I am getting a different exception generated from somewhere else. I got this exception:
Exception in thread "main" japa.parser.TokenMgrError: Lexical error at line 1, column 16. Encountered: "#" (35), after : ""
at japa.parser.ASTParserTokenManager.getNextToken(ASTParserTokenManager.java:2247)
at japa.parser.ASTParser.jj_ntk(ASTParser.java:9986)
at japa.parser.ASTParser.ClassOrInterfaceBody(ASTParser.java:926)
at japa.parser.ASTParser.ClassOrInterfaceDeclaration(ASTParser.java:604)
at japa.parser.ASTParser.TypeDeclaration(ASTParser.java:524)
at japa.parser.ASTParser.CompilationUnit(ASTParser.java:269)
at japa.parser.JavaParser.parse(JavaParser.java:81)
at japa.parser.JavaParser.parse(JavaParser.java:94)
at misc.CompileTest.main(CompileTest.java:45)
Any idea, how to handle the exception? Thanks in advance
As the name indicates, TokenMgrError is an error. So you have to catch an Error instead of Exception. If you want to catch both Error and Exception, you can use Throwable instead.
Originally, this error is throwed by JavaCC (TokenMgrError) which is used by Javaparser.
From version 3 on, JavaParser will/should not throw this error anymore.

Eclipse IIntroPart throwing exception when trying to load ActionBars

I have a class implementing IIntroPart and inside the creation method-
public void createPartControl(Composite container) {
Browser browser = new Browser(container,SWT.NONE);
browser.setUrl(STRHTMLPATH+File.separator+"wc.html");
IActionBars bars = getIntroSite().getActionBars(); //NULL POINTER
First two lines are working perfectly alright and its loading the browser as well, but when I am trying to load the actionBars as I do in View, I am getting NullPointerException here.
What is the issue.
Unable to create view ID org.eclipse.ui.internal.introview: An
unexpected exception was thrown.
Above is the message thrown before the Exception Stack

How to get details of ClientResponseFailure in RestEasy Client?

How to get http response contents when status >=400 is returned. That's my code sample :
try {
ChatService client = ProxyFactory.create(ChatService.class, apiUrl);
client.putMessage(dto);
} catch (ClientResponseFailure ex) {
System.out.println(ex.getResponse().getEntity().toString());
}
This throws :
Exception in thread "main" org.jboss.resteasy.spi.ReaderException: java.io.IOException: Stream closed
at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:123)
at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:246)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:210)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:171)
at App.main(App.java:40)
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream.read(SelfExpandingBufferredInputStream.java:58)
at java.io.FilterInputStream.read(FilterInputStream.java:90)
at org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream.read(SelfExpandingBufferredInputStream.java:68)
at org.jboss.resteasy.util.ReadFromStream.readFromStream(ReadFromStream.java:30)
at org.jboss.resteasy.plugins.providers.ByteArrayProvider.readFrom(ByteArrayProvider.java:32)
at org.jboss.resteasy.plugins.providers.ByteArrayProvider.readFrom(ByteArrayProvider.java:23)
at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:46)
at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)
at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:111)
... 4 more
I'd like to have more details than just status code 400.
Is that the exception you meant to send?
Unfortunately the RestEASY client framework doesn't support Exception marshalling per se, and instead adapts it into the HTTP framework. Exceptions should still be thrown on the server though. I've never done it, you can use ExceptionMappers for checked Exceptions.
http://docs.jboss.org/resteasy/docs/1.2.GA/userguide/html/ExceptionHandling.html
When debugging I noticed that the details I needed were in the 'streamFactory' object as a byte stream of XML. I found this help topic in the RestEasy docs about ClientResponse. It says
getEntity(java.lang.Class<T2> type)
where getEntity can marshal the output to the desired class. In my case, I have a custom class for errors returned from services called ServiceError. So, that was the class I passed to getEntity:
try {
serviceResult = proxy.addCustomer(customerName, customerProfile);
} catch (ClientResponseFailure ex) {
ClientResponse<String> cResp = ex.getResponse();
ServiceError myEntity = cResp.getEntity(ServiceError.class);
System.out.println("myEntity errorText=" + myEntity.getErrorMessage().getErrorText());
System.out.println("myEntity errorCode=" + myEntity.getErrorMessage().getErrorCode());
}