REST/json Angular/Grails HTTP PUT (update) "Internal Server Error" 500 - json

I had tested a sample app with REST/json/Grails on one end & Angular on one end to proof test the flow. It was working perfectly fine. All of HTTP GET, POST, PUT, & DELETE was fine.
Now I am making JSON requests via REST in a full-blown app for Project Management. It has basic class structure for the app: classes on Project, Contract, Contacts, Projects Roles etc.
While updating any & every record in the database (MySql 5.6 Community, Grails 2.4.3) via HTTP PUT (with PUT only, POSt with identical arguments works perfectly fine) REST/JSON, I am getting the following error (on the client side):
===================================================
Error 500: Internal Server Error URI/apaNew/api/v1/projectTasks/3Classjava.lang.NullPointerExceptionMessagenull
Around line 198 of PageFragmentCachingFilter.java 195: if (CollectionUtils.isEmpty(cacheOperations)) {196: log.debug("No cacheable annotation found for {}:{} {}",197: new Object[] { request.getMethod(), request.getRequestURI(), getContext() });198: chain.doFilter(request, response);199: return;200: }201:
Around line 63 of AbstractFilter.java 60: try {61: // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping62: if (filterNotDisabled(request)) {63: doFilter(request, response, chain);64: }65: else {66: chain.doFilter(req, res);
Around line 82 of CorsFilter.java 79: }80: }81: }82: filterChain.doFilter(request, response);83: }84:85: private boolean checkOrigin(HttpServletRequest req, HttpServletResponse resp) {
Trace
Line | Method ->> 198 | doFilter in PageFragmentCachingFilter.java
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 63 | doFilter in AbstractFilter.java
| 82 | doFilter in CorsFilter.java
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker ^ 745 | run in java.lang.Thread
==============================================
The application is a very simple project management app. It has a Project class whose records I am trying to modify.
It was working absolutely fine in a sample app I had written before to test everything. Only after converting it into a full-blown app the problem has surfaced.
The HTTP POST/GET/DELETE request is working perfectly fine. An HTTP POST request with similar parameters is working perfectly fine.

Related

Getting a reference error on gatsby build

I’ve been working on my project as usual, it was working fine. When I tried to do gatsby build it got this error message:
ReferenceError: Cannot access '_404' before initialization
and
ReferenceError: Cannot access '_mdx_slug_' before initialization
I haven’t added anything that should cause this, though I don’t really understand where the problem is coming from. It seems to refer to the way gatsby builds the pages of the website but I’m not experienced enough to know for sure.
gatsby build full:
success load gatsby config - 0.041s
success load plugins - 0.757s
success onPreInit - 0.029s
success initialize cache - 0.046s
success copy gatsby files - 0.183s
success Compiling Gatsby Functions - 0.185s
success onPreBootstrap - 0.201s
success createSchemaCustomization - 0.011s
success Checking for changed pages - 0.001s
success source and transform nodes - 0.511s
info Writing GraphQL type definitions to /home/daniel_gray/GatsbyProjects/watchsdarot/.cache/schema.gql
warn There are conflicting field types in your data.
If you have explicitly defined a type for those fields, you can safely ignore this warning message.
Otherwise, Gatsby will omit those fields from the GraphQL schema.
If you know all field types in advance, the best strategy is to explicitly define them with the `createTypes` action, and skip inference with the `#dontInfer` directive.
See https://www.gatsbyjs.com/docs/actions/#createTypes
Mdx.frontmatter.EpisodeNum:
- type: number
value: 10
- type: string
value: '09'
Mdx.frontmatter.NumOfEpisodes:
- type: number
value: 10
- type: string
value: '09'
success building schema - 0.469s
success createPages - 0.002s
success createPagesStatefully - 0.336s
info Total nodes: 329, SitePage nodes: 86 (use --verbose for breakdown)
success Checking for changed pages - 0.005s
success Cleaning up stale page-data - 0.010s
success onPreExtractQueries - 0.001s
success extract queries from components - 2.813s
success write out redirect data - 0.010s
success Build manifest and related icons - 0.565s
success onPostBootstrap - 0.609s
info bootstrap finished - 9.239s
success write out requires - 0.048s
success Building production JavaScript and CSS bundles - 8.175s
⠼ Building HTML renderer
[============================] 10.041 s 7/7 100% Running gatsby-plugin-sharp.IMAGE_PROCESSING jobs
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'mini-css-extract-plugin /home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[10].oneOf[0].use[1]!/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[10].oneOf[0].use[2]!/home/daniel_gray/GatsbyProjects/watchsdarot/src/components/footer.module.css|0|Compilation/modules|/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[10].oneOf[0].use[1]!/home/daniel_gray/GatsbyProjects/watchsdar
⠴ Building HTML renderer
[============================] 10.041 s 7/7 100% Running gatsby-plugin-sharp.IMAGE_PROCESSING jobs
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[9].oneOf[0].use[0]!/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[9].oneOf[0].use[1]!/home/daniel_gray/GatsbyProjects/watchsdarot/src/components/foote
success Building HTML renderer - 22.328s
success Execute page configs - 0.028s
success Caching Webpack compilations - 0.002s
success run queries in workers - 0.133s - 78/78 585.57/s
success Running gatsby-plugin-sharp.IMAGE_PROCESSING jobs - 32.550s - 7/7 0.22/s
success Merge worker state - 0.002s
success Rewriting compilation hashes - 0.002s
success Writing page-data.json files to public directory - 0.048s - 79/79 1656.57/s
[ ] 0.000 s 0/85 0% Building static HTML for pages
/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/yoga-layout-prebuilt/yoga-layout/build/Release/nbind.js:53
throw ex;
^
ReferenceError: Cannot access '_404' before initialization
at Module.default (/home/daniel_gray/GatsbyProjects/watchsdarot/.cache/page-ssr/routes/component---src-pages-404-js.js:182:35)
at RouteHandler.render (/home/daniel_gray/GatsbyProjects/watchsdarot/.cache/page-ssr/routes/webpack:/watchsdarot/.cache/static-entry.js:229:57)
at Ic (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:66:30)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:68:210)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:72:13)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Lc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:77:98)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:69:131)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Ic (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:66:373)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:68:210)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:72:271)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:68:481)
Emitted 'error' event on WritableAsPromise instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
at runNextTicks (node:internal/process/task_queues:65:3)
failed Building static HTML for pages - 2.072s
/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/yoga-layout-prebuilt/yoga-layout/build/Release/nbind.js:53
throw ex;
^
ReferenceError: Cannot access '_mdx_slug_' before initialization
at Module.default (/home/daniel_gray/GatsbyProjects/watchsdarot/.cache/page-ssr/routes/component---src-pages-player-mdx-slug-js.js:278:35)
at RouteHandler.render (/home/daniel_gray/GatsbyProjects/watchsdarot/.cache/page-ssr/routes/webpack:/watchsdarot/.cache/static-entry.js:229:57)
at Ic (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:66:30)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:68:210)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:72:13)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Lc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:77:98)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:69:131)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Ic (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:66:373)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:68:210)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:72:271)
at Z (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:74:89)
at Kc (/home/daniel_gray/GatsbyProjects/watchsdarot/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:68:481)
Emitted 'error' event on WritableAsPromise instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
at runNextTicks (node:internal/process/task_queues:65:3)
ERROR #95313
Building static HTML failed
See our docs page for more info on this error: https://gatsby.dev/debug-html
1 | 'use strict';
> 2 | module.exports = function (obj, predicate) {
| ^
3 | var ret = {};
4 | var keys = Object.keys(obj);
5 | var isArr = Array.isArray(predicate);
WebpackError: Worker exited before finishing task
- index.js:2
[watchsdarot]/[filter-obj]/index.js:2:1
- index.js:194
[watchsdarot]/[query-string]/index.js:194:1
- dev-404-page.js:209
watchsdarot/.cache/dev-404-page.js:209:19
Thanks to anyone tried to help.
Found a solution to it on my own. It was a certain call to document.documentElement that prevented the build process to create static HTML files. No idea still why it pointed to two different pages since it was on another page but everything seems to be working now. For anyone still struggling try wrapping your window or document calls in a useEffect() hook or an if statement like this if(typeof window !== 'undefined')
Hope this helps someone!
Good luck.

Save JSON as Domain Class with Restfull Controller in Deep

I've two domain class:
class A {
String foo
static hasMany = [children: B]
}
class B {
String bar
static belongsTo = [a: A]
}
I want to save A from JSON like:
{
foo: "Hello world!",
children: [
{bar: "First child!", _ref: "../.."},
{bar: "Second child!", _ref: "../.."}
]
}
(Note: If I don't put _ref, B objects are saved but without reference to A, ie, null values.)
And I have a Restfull Controller like:
class AController extends RestfullController {
static responseFormats = ['json', 'xml']
AController() {
super(A)
}
}
But, when I try to save my object, I got:
ERROR errors.GrailsExceptionResolver - TransientObjectException occurred when processing request: [POST] /example/a/save.json
object references an unsaved transient instance - save the transient instance before flushing: mx.gob.morelos.ppi.Proyecto. Stacktrace follows:
Message: object references an unsaved transient instance - save the transient instance before flushing: A
Line | Method
->> 99 | $tt__save in grails.rest.RestfulController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
What I need to do to save my JSON with their relations?
Regards.

Saving of nested list of objects fails with JSON post in Grails

Example application: https://github.com/Hi-Fi/writetable-example-with-grails
In example application, controllers work fine when used through GSP pages or posting with CURL the form. Command with curl:
curl "http://localhost:8080/junk/team/save" --data "name=team&players"%"5B0"%"5D.firstName=first&players"%"5B0"%"5D.lastName=last&players"%"5B0"%"5D.position=position&create=Create" --compressed.
Problem is with identical request using JSON, which creates identical (according dump()) object, but can't save the nested values.
When making following POST with SOAPUI (localhost:8080/junk/api/rest):
{
name: "Team name",
players: [
{
firstName: "Player first name",
lastName: "Player last name",
position: "Player position"
}
]
}
I just get error:
| Error 2014-12-20 21:02:47,050 [http-bio-8080-exec-1] ERROR hibernate.AssertionFailure - an assertion failure occured
(this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in junk.Player entry (don't flush the Session after an exception occurs)
Line | Method
->> 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run . . . in java.lang.Thread
Parent object (Team) is still saved even there's this error. With Hibernate4 whole save fails, so updating to it wasn't helping.
One way to solve this is explicitly connect "Team" with each "Player" like
team.players.each {it.team = team}
With form posting, this step is made automatically, but for some reason not with JSON. Player object at after Team object creation in JSON:
<junk.Player#76f8539e firstName=first lastName=last position=position deleted=false errors=grails.validation.ValidationErrors: 0 errors $changedProperties=null id=null version=null team=null>
And with form POST:
<junk.Player#1ba14c1b firstName=first lastName=last position=position deleted=false errors=grails.validation.ValidationErrors: 0 errors $changedProperties=null id=null version=null team=junk.Team : (unsaved)>
This might not be the "right" solution, so I'm still happy to hear if there's better solution for nested one-to-many -lists.

How to cast a JSONObjectMap into Map?

The following example I derived from
http://mrhaki.blogspot.de/2013/12/grails-goodness-rendering-partial.html
Domain
class Book {
String title
Map details
}
Controller
#Transactional
class BookApiController extends RestfulController {
static responseFormats = ['json', 'xml']
BookApiController() {
super(Book)
}
#Override
#Transactional
def save(Book bookInstance) {
println "details: " + bookInstance.details.getClass().name
bookInstance.save flush:true
respond bookInstance
}
}
URLMappings
"/api/book"(resources: "bookApi")
When I'm using this curl invocation:
curl -i -X POST -H "Content-Type: application/json" -d '{"title":"Grails in Action","details":{"isbn":"123456789"}}' http://localhost:8080/restpoint/api/book
Grails throws the following exception:
details: org.codehaus.groovy.grails.web.binding.bindingsource.JsonDataBindingSourceCreator$JsonObjectMap
| Error 2014-06-09 13:45:52,057 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver - ClassCastException occurred when processing request: [POST] /restpoint/api/book
com.google.gson.JsonPrimitive cannot be cast to java.lang.String. Stacktrace follows:
Message: com.google.gson.JsonPrimitive cannot be cast to java.lang.String
Line | Method
->> 23 | $tt__save in sample.core.BookApiController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 724 | run in java.lang.Thread
I need the map property (here called 'details') in my Book domain this is precondition.
A we can see it above the map is an instance of org.codehaus.groovy.grails.web.binding.bindingsource.JsonDataBindingSourceCreator$JsonObjectMap
How can I cast it into a map?
Any idea?
Thanks in advance.
-markus
This seems like a bug, please report a JIRA issue at http://jira.grails.org/browse/GRAILS

java.lang.ClassCastException in Grails app

I am following the free online book "Getting Started with Grails" (http://www.infoq.com/minibooks/grails-getting-started) and I am getting a java.lang.ClassCastException when trying to list any domain class. Can anyone decipher this?
URI: /RaceTrack/runner/list
Class: java.lang.ClassCastException
Message: sun.proxy.$Proxy26 cannot be cast to org.springframework.orm.hibernate3.HibernateCallback
Stack trace:
Line | Method
->> 15 | list in RunnerController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 186 | doFilter in PageFragmentCachingFilter.java
| 63 | doFilter in AbstractFilter.java
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 722 | run in java.lang.Thread
Additional info (around line 186 of PageFragmentCachingFilter.java)
183: if(method == null) {
184: log.debug("No cacheable method found for {}:{} {}",
185: new Object[] { request.getMethod(), request.getRequestURI(), getContext() });
186: chain.doFilter(request, response);
187: return;
188: }
189: Collection<CacheOperation> cacheOperations = cacheOperationSource.getCacheOperations(
Additional info (around line 63 of AbstractFilter.java):
60: try {
61: // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping
62: if (filterNotDisabled(request)) {
63: doFilter(request, response, chain);
64: }
65: else {
66: chain.doFilter(req, res);
I've had the same issue happening all of a sudden couple days back. Deleting ~/.grails/2.0.4/.slcache/ directory fixes it for me.
Delete .slcache in both the top of the .grails subdirectory and also the .slcache, if it exists, for the particular version of grails being used. For example, ~/.grails/2.1.3/.slcache.
This worked when using IntelliJ IDEA to launch the app.
Does the app start up with reloading (spring-loaded agent) disabled?
grails -noreloading run-app
A similar problem has been reported to Grails Jira as GRAILS-9952. It would help fixing the problem if you can provide an test app that reproduces the problem. Please attach that to the jira issue.