Error occurred initializing command object on succeeding command objects in controller - grails

I have the defined following controller
class BookController {
def book(BookCommand bookCommand,
AnotherBookCommand bookCommand2)
{
....
}
When I debugged it, binding is done on the bookCommand and when it's bookCommand2's turn for binding, it throws this exception
Error in object 'bookCommand2': codes []; arguments []; default message [Error occurred initializing command object [bookCommand2]. org.apache.groovy.json.internal.Exceptions$JsonInternalException: Wrapped Exception
CAUSE java.io.IOException :: Stream closed]
If I try to switch the order of the parameters i.e.
class BookController {
def book(AnotherBookCommand bookCommand2,
BookCommand bookCommand)
{
....
}
Binding is done on the bookCommand2 and binding of bookCommand throws the exception.
Error in object 'bookCommand': codes []; arguments []; default message [Error occurred initializing command object [bookCommand]. org.apache.groovy.json.internal.Exceptions$JsonInternalException: Wrapped Exception
CAUSE java.io.IOException :: Stream closed]
Any idea what's happening here?

We do not support binding the request of the body to multiple command objects. One option you have is to aggregate them into 1 class with something like this:
class SomeClass {
BookCommand bookCommand
BookCommand anotherBookCommand
}
Then have your controller action accept one of those and organize the body of the JSON accordingly.

Related

catch any error in angular dart (like angular's ErrorHandler)

I need to catch any front end (angulardart) error and send it back to the server.
I saw there is something like his in regular Angular ErrorHandler, but I can't find any equivalent in angular dart (or dart it self).
Maybe I should hack the Exception object's constructor, but I don't find it a good approach (assuming it's possible)
any hints please?
In Dart it's quite similar:
#Injectable()
class ErrorHandler implements ExceptionHandler {
ApplicationRef _appRef;
ErrorHandler(Injector injector) {
// prevent DI circular dependency
new Future<Null>.delayed(Duration.ZERO, () {
_appRef = injector.get(ApplicationRef) as ApplicationRef;
});
}
#override
void call(dynamic exception, [dynamic stackTrace, String reason]) {
final stackTraceParam = stackTrace is StackTrace
? stackTrace
: (stackTrace is String
? new StackTrace.fromString(stackTrace)
: (stackTrace is List
? new StackTrace.fromString(stackTrace.join('\n'))
: null));
_log.shout(reason ?? exception, exception, stackTraceParam);
// We can try to get an error shown, but don't assume the app is
// in a healthy state after this error handler was reached.
// You can for example still instruct the user to reload the
// page with danger to cause hare because of inconsistent
// application state..
// To get changes shown, we need to explicitly invoke change detection.
_appRef?.tick();
}
}
Provide the error handler
return bootstrap(AppComponent, [const Provide(ExceptionHandler, useClass: ErrorHandler)]);
For errors that might be caused outside Angular, see also How to catch all uncaught errors in a dart polymer app?

Grails 2.4.3 - Asynchronous Programming #DelegateAsync

Suppose the following situation...
I have to perform a process which takes a lot of time (about 1 hour)
and I do not want the user of my application to wait until it ends.
The business logic of this process is encapsulated in several services.
So:
class MyService{
def myService2
def myService3
public doSomething(){
myService2.doSomething()
myService3.doSomething()
}
}
class MyController{
def myService
def anAction(){
myService.doSomething()
redirect(action:'index')
}
}
I want the call to my service to be asynchronous.
I tried to create an asynchronous service as indicated by the Grails documentation, using #DelegateAsync.
class AsyncMyService {
#DelegateAsync MyService myService
}
class MyController{
def asyncMyService
def anAction(){
asyncMyService.doSomething()
.onComplete { List results ->
println "completed!"
}
redirect(action:'index')
}
}
But I get the following error when compiling my application:
Caused by ConversionNotSupportedException: Failed to convert property value of type
'MyService2$$EnhancerBySpringCGLIB$$f08a1b38' to equired type 'grails.async.Promise'
for property 'myService2'; nested exception is java.lang.IllegalStateException: Cannot
convert value of type [MyService2$$EnhancerBySpringCGLIB$$f08a1b38] to required type
[grails.async.Promise] for property 'myService2': no matching editors or conversion
strategy found
Any ideas to solve this problem?
Thanks!

Codeception injection fails using page object

I'm new to Codeception, but I'm running into an issue injecting Page Objects. The problem occurs when I add the following construct logic to my page object.
public function __construct(\AcceptanceTester $I) {
$this->tester = $I;
}
... I got this from Login Page object example here: http://codeception.com/docs/06-ReusingTestCode#PageObjects
The error I'm getting is:
[Codeception\Exception\InjectionException]
Failed to inject dependencies in instance of 'MyCest'. Failed to create instance of 'Page\Login'. Failed to create instance of 'AcceptanceTester'. Failed to create instance of 'Codeception\Scenario'. Failed to resolve dependency 'Codeception\TestCase'.
This is how I'm injecting the page in my Cest.
protected function _inject(\Page\Login $login) {
$this->login_page = $login;
}
If I remove the __construct code, the error goes away. Is this a bug in Codeception or am I doing something wrong?
This is the work-around I found...
use \AcceptanceTester;
use Page\Login as LoginPage;
class MyCest {
protected $login_page;
public function _before(AcceptanceTester $I) {
$this->login_page = new LoginPage($I);
}
}
It is expected behaviour.
Your LoginPage constructor should not have any arguments to be instantiated during DI, so your workaround is right way to initialize LoginPage instance with AcceptanceTester instance.
When you specify AcceptanceTester as LoginPage's ctor arg DI mechanism of Codeception tries resolve dependencies recursively in the following way:
LoginPage(AcceptanceTester) -> AcceptanceTester(Scenario) -> Scenario(TestCase) -> TestCase
but TestCase is abstract class so it can not be instantiated.

Error casting map to grails.gorm.CriteriaBuilder

Could someone please tell me why I am not able to mock CriteriBuilder in my test. When criteriaBuilderMock.createMock() is executed I am getting Error casting map to grails.gorm.CriteriaBuilder, Reason: null
Is it possible to mock CriteriaBuilder? Am I getting this error because grails does not allow mocking objects without default constructor? Is there work around?
#TestFor(ProductService)
class ProductServiceSpec extends Specification {
void "test exists"() {
given:
def criteriaBuilderMock = mockFor(CriteriaBuilder);
criteriaBuilderMock.createMock(); //<-- error org.codehaus.groovy.runtime.typehandling.GroovyCastException: Error casting map to grails.gorm.CriteriaBuilder, Reason: null
}
}
thanks
The mocked class has to have a no-args constructor.

Exception handling with dynamic mixins in Grails controller

I am using dynamic mixins on my Grails 2.3.11 controllers according to this proposal on SO, for DRY reasons:
class SomeApiController {
def SomeApiController() {
SomeApiController.mixin MyControllerMixin
}
...
Unfortunately it does not work for exception handlers, like:
class MyControllerMixin {
...
def businessException( BusinessException e ) {
log.error( "API exception: ${e.message} ${e.errorCode}", e )
def result = [
status: 'Failure',
errorCode: e.errorCode.name()
]
response.status = 400
render result as JSON
}
...
}
In this case, the default exception handler in SomeApiController is called when a BusinessException is thrown from somewhere. It works nicely if I place the handler directly in the controller, though, but this is what I want to avoid obviously.
Is there any workaround to get that working as well?
See http://grails.org/doc/latest/guide/theWebLayer.html#controllerExceptionHandling. That includes the following...
Exception handler methods must be present at compile time.
Specifically, exception handler methods which are runtime
metaprogrammed onto a controller class are not supported.

Resources