Skip to content
shackbarth edited this page Jun 3, 2013 · 2 revisions

Sometimes during development you will run across an error in the console that looks something like

Response error  
Object {isError: true, error: Object, message: "Error: null value in column "grppriv_grp_id" violates not-null constraint", description: "Error: null value in column "grppriv_grp_id" violates not-null constraint"}
```

which might expand to

```
Response error  
Object {isError: true, error: Object, message: "Error: null value in column "grppriv_grp_id" violates not-null constraint", description: "Error: null value in column "grppriv_grp_id" violates not-null constraint"}
description: "Error: null value in column "grppriv_grp_id" violates not-null constraint"
error: Object
code: "XX000"
detail: "undefined() LINE 525:       plv8.execute(sql.statement, sql.values);"
file: "plv8.cc"
line: "1428"
routine: "rethrow"
severity: "ERROR"
__proto__: Object
isError: true
message: "Error: null value in column "grppriv_grp_id" violates not-null constraint"
__proto__: Object
```

This is what errors look like when they come from the database, and in order to get to the bottom of them it is sometimes necessary to get your hands dirty in our plv8 layer. Here's how to do it.


***


If you have postgres running in the foreground, or if you can access the logs, you should be able to see a more descriptive error message.

```
ERROR:  Error: null value in column "grppriv_grp_id" violates not-null constraint
DETAIL:  undefined() LINE 525:       plv8.execute(sql.statement, sql.values);
STATEMENT:  select xt.post($${"databaseType":"instance","nameSpace":"XM","type":"UserAccountRole","id":"LIMITED","data":{"grantedPrivileges":[{"privilege":"MaintainTaxZones","uuid":"ac0c200a-07d6-4261-84e6-6fd60d703a6e"},{"privilege":"MaintainTaxTypes","uuid":"622193af-c39c-4f8d-b89b-faa61ccf6df8"},{"privilege":"MaintainTaxRegistrations","uuid":"7a48768b-6f9e-4822-c50e-cdf3e281d120"},{"privilege":"MaintainTaxReconciliations","uuid":"d1eece44-2cd9-44a6-c9d4-e4aea87f643b"},{"privilege":"MaintainTaxClasses","uuid":"8dc2ea1d-e447-4569-a377-525bdbbf892c"},{"privilege":"MaintainTaxAssignments","uuid":"266d908e-3977-47f8-e0d2-ebd5a88fe5e3"},{"privilege":"ViewTaxAssignments","uuid":"8d24fc88-5a02-4211-8ad3-2c6bb68da08b"},{"privilege":"ViewTaxClasses","uuid":"85c5d592-fc60-4539-e21c-3df6bf56be07"},{"privilege":"ViewTaxReconciliations","uuid":"9fb4fa33-fbcf-473f-9d76-8466e52e802f"},{"privilege":"ViewTaxRegistrations","uuid":"4110c5b2-fbbe-4034-f02c-774fb774d1c2"},{"privilege":"ViewTaxTypes","uuid":"1fa8a4ce-2fbb-4a41-e916-7c7fa9313712"},{"privilege":"ViewTaxZones","uuid":"3a1f173b-167f-4272-95ac-2065190d99cb"}],"name":"LIMITED"},"binaryField":null,"username":"admin"}$$)
```

That select statement represents the query that the datasource is running against the database. Sometimes it is not possible to see the query from the postgres log. In this case, you should set the debugging flag to true in the node-datasource/config.js and restart the datasource, at which point all of the queries to the database will be printed to the node-datasource console (or log, if you are running the datasource in the background).

We can debug further by using pgadmin3 (or psql if you are too hip for GUI). Paste the query (including the word `select` but not including the word `STATEMENT`) into a SQL editor after the following lines:

```
select xt.js_init(true);
select xt.post($${ ... }$$)
```

And you should be able to see logs from the database in your Messages tab. 


***


Unfortunately, we don't have a way to step-debug in plv8, so in order to see the inner workings you have to sprinkle in your own log statements, which in plv8 look something like 

```javascript
plv8.elog(NOTICE, "Here are my objects", myObject, mySecondObject, myThirdObject);
```

To edit the plv8 SQL files, simply use pgadmin to "Open file", make your changes, and "Execute query". Then on your original window you should be able to rerun the query and look at the messages tab. If you make a fix to the plv8 file you can save it straight from pgadmin, otherwise you can throw away your debug logs by closing without saving.

It is sometimes tricky to determine in which file the error is happening, because the error message `detail: "undefined() LINE 525:       plv8.execute(sql.statement, sql.values);"` gives you the line number and the actual line of source code, but not the name of the file. Grep is typically your best bet in this case.