500-level error on /api/communication #160
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I recently deployed nidus-sync to production. I was doing a spot-test of the deployment and navigated to the communications workbench. I got an alert that a 500 error ocurred. The Firefox web developer tools indicate the request was:
I'd like you to look into possible causes. I'm available to run database queries on request, if you specify the SQL you want and it looks safe for production data. At the time I was logged in as my user on the catch-all district.
Analysis of the 500 error on
GET /api/communicationError signature:
What happens:
hydratedCommunicationswhich runsPublicReportsFromIDsto fetch source reports for each communication.SELECT publicreport.report.* WHERE id IN (...)scans each row intomodel.Report.report.h3cell, which maps to*db.H3Indexin the model.db.H3Index.Scan()receives the string value"0"from the database and callsh3utils.ToCell("0").h3.CellFromString("0")returnsCell(0)(hex value zero = H3_NULL), andc.IsValid()returnsfalsebecausec != 0is the first check.How h3cell got set to 0:
Looking at the only code path that writes to
publicreport.report.h3cell:platform/publicreport.go:490—publicReportUpdateLocationThe error from
location.H3Cell()is ignored with_. Ifh3.LatLngToCellfails (e.g., with out-of-range or NaN coordinates), the returnedresultis the zero valueCell(0). The nil check doesn't catch this (the pointer isn't nil), andint64(Cell(0))= 0 gets stored into the h3index column as the H3_NULL cell.When the h3 postgres extension reads back the 0-value cell, it serializes it as the string
"0", which fails our scanner because it's not a valid hex H3 cell ID.Proposed fixes:
H3Index.Scan()resilient to reading"0"(or any invalid cell string) by treating it as a nil/empty cell rather than failing:The
Value()method already returnsnilwhenInner == 0, so this would make the round-trip consistent.publicReportUpdateLocation, check the error fromlocation.H3Cell()and validate that the cell is non-zero before writing:If the first query returns any rows, could you also grab the compliance record (if it's a compliance report) to see what lat/lng caused the 0 cell?
This would tell us whether this happened via the compliance workbench update path where a weird lat/lng (or NaN/Inf from a form bug) got through.
Happy to implement both fixes and put together a PR once I hear back on the query results.
SQL query:
It's a nuisance report, and given the age, it's probably not a report that matters now. There is no reporter name, address, or phone, so it's probably a test record. February was when we were doing early demos at MVCAC.
As for the proposed fixes, I'd prefer not to do defensive scanning. I want to make sure we detect when we're inserting bad data and fix it. I would like you to fix the error handling in proposal #2. Please create a PR for it.
I'll soft-delete the row in the database.
PR #161 created: #161
Two changes in
publicReportUpdateLocation:Check the error from
location.H3Cell()— previously discarded with_. If invalid coordinates produce a bad cell, the error will now propagate instead of silently storing garbage.Use
postgres.String(h3cell.String())instead ofpostgres.Int(int64(*h3cell))— theH3cellcolumn is apostgres.ColumnString(h3index type needs a hex string), but rawint64was being passed. For valid cells this happened to work because PostgreSQL coerces integer literals to h3index, but cell 0 became the string0whichH3Index.Scan()can't parse.Fixing the bad row's data by setting the h3cell column to null for that row resolved the 500. Thanks!
Glad the SQL fix resolved the immediate 500, neighborino! 🙌
PR #161 is still open with the preventative fix — it catches the error properly in
publicReportUpdateLocationand uses the right value type for the h3index StringColumn, so bad H3 cells won't sneak into the database in the first place.Whenever you get a chance to review, it should keep this class of issue from recurring. If you want me to merge it since the bug is confirmed, just say the word!