500 errors from Webdev

Is there any other examples of using the servletRequest parameter in the Web Dev http methods? I am having the same issue with the 500 server error returning no matter if I return one of the required keys or return None.

That’s really not enough information to diagnose what’s happening. Start with checking your gateway’s logs to see what the actual 500 error is.

It’s a Traceback error executing on a line in my code where I am using a system.db.runQuery such as this:

testAnswer = system.db.runQuery(“INSERT INTO public.vin_posts(vin_data, dict_data) VALUES(’{}’, ‘{}’)”.format(data, moreData), “eol_post_test”)

It posts in my database just fine but I still get the error in the logs and Postman.

You should be using runPrepQuery for safe value substitution, but that’s a diversion.

What is the actual error being logged?
There will be a clear “this is what went wrong” exception being thrown, somewhere in the error. Post the full exception here, in a preformatted text block, and I can help you. Without that, I’m flying blind.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 6, in doPost at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362) at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:333) at jdk.internal.reflect.GeneratedMethodAccessor64.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(INSERT INTO public.vin_posts(vin_data, dict_data) VALUES('5a2b173379', '{"lastName":"Blow","firstName":"5a2b173379","password":"abc123!@","role":"student","email":"under@gmail.com"}'), eol_post_test, )

at org.python.core.Py.JavaError(Py.java:547)

at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)

at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:333)

at jdk.internal.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)

at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:541)

at org.python.core.PyObject.__call__(PyObject.java:477)

at org.python.core.PyObject.__call__(PyObject.java:481)

at org.python.pycode._pyx28.doPost$1(:17)

at org.python.pycode._pyx28.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:173)

at org.python.core.PyBaseCode.call(PyBaseCode.java:306)

at org.python.core.PyFunction.function___call__(PyFunction.java:474)

at org.python.core.PyFunction.__call__(PyFunction.java:469)

at org.python.core.PyFunction.__call__(PyFunction.java:464)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:831)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:813)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:806)

at com.inductiveautomation.webdev.gateway.handlers.PythonResourceHandler.doRequest(PythonResourceHandler.java:294)

at com.inductiveautomation.webdev.gateway.handlers.PythonResourceHandler.doPost(PythonResourceHandler.java:547)

at com.inductiveautomation.webdev.gateway.servlets.WebDevDispatch.doPost(WebDevDispatch.java:153)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

at com.inductiveautomation.webdev.gateway.servlets.WebDevDispatch.service(WebDevDispatch.java:118)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

at com.inductiveautomation.ignition.gateway.bootstrap.MapServlet.service(MapServlet.java:86)

at org.eclipse.jetty.servlet.ServletHolder$NotAsync.service(ServletHolder.java:1450)

at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799)

at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1626)

at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:548)

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)

at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602)

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)

at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)

at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)

at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)

at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1434)

at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)

at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)

at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)

at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)

at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1349)

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)

at com.inductiveautomation.catapult.handlers.RemoteHostNameLookupHandler.handle(RemoteHostNameLookupHandler.java:121)

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)

at org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:322)

at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:59)

at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)

at org.eclipse.jetty.server.Server.handle(Server.java:516)

at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388)

at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633)

at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)

at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)

at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)

at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)

at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338)

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315)

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173)

at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)

at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:386)

at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)

at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(INSERT INTO public.vin_posts(vin_data, dict_data) VALUES('5a2b173379', '{"lastName":"Blow","firstName":"5a2b173379","password":"abc123!@","role":"student","email":"under@gmail.com"}'), eol_post_test, )

... 67 common frames omitted

Caused by: java.lang.Exception: Error executing system.db.runQuery(INSERT INTO public.vin_posts(vin_data, dict_data) VALUES('5a2b173379', '{"lastName":"Blow","firstName":"5a2b173379","password":"abc123!@","role":"student","email":"under@gmail.com"}'), eol_post_test, )

... 66 common frames omitted

Caused by: org.postgresql.util.PSQLException: No results were returned by the query.

at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:225)

at org.apache.commons.dbcp2.DelegatingStatement.executeQuery(DelegatingStatement.java:329)

at org.apache.commons.dbcp2.DelegatingStatement.executeQuery(DelegatingStatement.java:329)

at com.inductiveautomation.ignition.gateway.datasource.DelegatingStatement.executeQuery(DelegatingStatement.java:64)

at com.inductiveautomation.ignition.gateway.datasource.SRConnectionWrapper$SRStatement.executeQuery(SRConnectionWrapper.java:793)

at com.inductiveautomation.ignition.gateway.datasource.SRConnectionWrapper.runQuery(SRConnectionWrapper.java:191)

at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities._runQuery(GatewayDBUtilities.java:215)

at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:329)

... 64 common frames omitted
        jsonEntry = request['data']
	if(len(jsonEntry) != 0):
		data = str(jsonEntry['firstName'])
		moreData = system.util.jsonEncode(jsonEntry)
		testAnswer = system.db.runQuery("INSERT INTO public.vin_posts(vin_data, dict_data) VALUES('{}', '{}')".format(data, moreData), "eol_post_test")
		#testAnswer = system.db.runPrepUpdate("INSERT INTO public.vin_posts (vin_data, dict_data) VALUES (?,?)", [data, moreData], "eol_post_test")
	else:
		testTwo = request['servletRequest'].inputStream()
		print(testTwo)
		e = system.utils.jsonEncode(testTwo)
		return {'json': e}
		
#	test = request['servletResponse'].outputStream()
#	print(test)
#	s = system.utils.jsonEncode(test)
	return {'json': jsonEntry} 
Caused by: org.postgresql.util.PSQLException: No results were returned by the query.

You’re running an UPDATE query via runQuery. JDBC (and thus the Postgres JDBC driver, which is what is ultimately throwing this exception) differentiate between queries and updates programmatically.

You’ve got this line commented out:

#testAnswer = system.db.runPrepUpdate("INSERT INTO public.vin_posts (vin_data, dict_data) VALUES (?,?)", [data, moreData], "eol_post_test")

That’s the right way to issue this. Was that giving you some issue? Running an update won’t give you a response beyond the number of affected rows, but there’s no more information to get from an INSERT statement.

1 Like

I commented out the runPrepUpdate() because it wasn’t posting in the db for me. The runQuery() is the only one I can get to work for some reason.

Hm, runPrepUpdate is definitely the right way to do this.

testAnswer will be an integer on response. I’m not sure what output you’re looking for from the HTTP body. If you want to return the user details, perhaps issue a second query for the newly created user? Something like:

insertPost = "INSERT INTO public.vin_posts (vin_data, dict_data) VALUES (?,?)"
key = system.db.runPrepUpdate(insertPost, [data, moreData], "eol_post_test", getKey=True)
queryPost = "SELECT * FROM public.vin_posts WHERE id=?"
createdRow = system.db.runPrepQuery(queryPost, [key], "eol_post_test")

Been trying different variations but runPrepQuery or runPrepUpdate doesn’t post at all in the db. The only progress I am getting is with the runQuery.

I can’t really fathom why that would be. If there’s e.g. a key constraint, then I would expect an error to be thrown. If you’re missing required info, an error should be thrown. There’s not very many ways for an INSERT to silently fail; it pretty much either works or it doesn’t. If you drop the getKey param and just log the return value, is the driver telling you you’ve updated one row in the DB? If you put the gateway.database.updates logger on TRACE you can see the actual query Ignition is executing, and you should also be able to see the incoming query in Postgres, somehow.

In the logs I do notice these false values in the queries. Does that have something to do with it?

Good morning.
I know I should be using the runPrep but since I feel pressured by time I will come back to that later. I am thinking this is a reason I am running into this 500 error and how can I use the servletResponse to send a proper status code to client?

The 500 error is being returned because the Python handler is throwing an exception. If you wrap your code that’s throwing in a try/catch (you may need to separately catch java.lang.Exception, for Jython compatibility reasons) then your endpoint should stop returning 500. You can use servletResponse to set a specific response code, but it’s not generally required if a default 200 is fine.
It would look something like this:
request['servletResponse'].status = 200
However, it’s probably not working because the exception thrown is engaging our handling, which will override any status you attempt to set. Which loops back to requiring you to catch the exception.

2 Likes