Web Development with Clojure, Third Edition: correct reference code (page 116)

In general, how do I correctly find the reference code that matches each book topic?

More specifically, I am having problems around page 116 testing that the code still works after setting up shadow-cljs. I tried using the reference code called “guestbook-shadow” and still had the same problems. Maybe I am using the wrong reference code. The problems I see are…

  • I don’t always see the “WebSocket connected!” in the browser. I have not figured out yet exactly when this happens.
  • GET /api/messages response is always 500 “Internal Server Error”
  • The page never gets passed “Loading Messages…”
  • Sometimes I get this error about invalid gy flags in xregexp.
  • I get some error about protocol_mask partition.
  • The browser console shows errors about formatters not being installed and another about custom formatters not being rendered.
  • On the positive side, the browser console reports App initilized and components mounted and shadow-cljs ready.

How do I debug this? A server-side unit test of get-messages responds with 200, so I think the problem is in middleware or the websocket config. I was hoping the reference code would help, but assuming I have the right one, it is the same, with exception of some SQL files.

I am enjoying the book very much & looking forward to getting past this hump. Thanks for your hard work.

Regards,
Pete

1 Like

You can find the reference code here under the “Resources” section.

The “WebSocket Connected” message not appearing is likely due to shadow-cljs being inactive or still compiling. You can try getting more info from the browser console, or check the CLI output for a message that looks like [:app] Build Completed. (...)

The 500 from /api/messages could be plenty of things. Validating the get-messages function is a good start, that means it’s likely an issue with the route itself. Check the logs for an error, and if that’s not descriptive, add a log statement at the very beginning of your handler fn to make sure you enter it. If not, it’s likely an issue with the :parameters or :responses validation, but it could be another route issue. If you do get the log statement, wrap the handler body in a try/catch and log the error before re-throwing to get more info. Sometimes the error can get obfuscated by the middleware stack.

The page never getting past “loading messages…” is a result of the 500 most likely, since they’re failing to load.

The gy flags in xregexp is a version issue. Try bumping the version and removing core-js dependency.

I am not sure what the protocol_mask partition issue is, so that would need some further investigation. Sorry I can’t be helpful there. Try seeking out help from shadow-cljs documentation or open an issue on the shadow-cljs github repository if you can’t resolve it. https://github.com/thheller/shadow-cljs

The formatters sounds like an issue with cljs-devtools chrome plugin. It shouldn’t be an issue with the application itself.

Good luck with the debugging. Share your logs and relevant code if you’re still stuck.

Glad to hear you’re enjoying the book so far, thanks for reading!

1 Like

Thanks for such a detailed response Scot! It really helped me get back on track. Everything is behaving as expected now.

With the try/catch suggestion, I figured out the database was corrupted or missing. Once re-generated, I was at least getting some data back. I couldn’t successfully regenerate just by using the repl and commands in env/dev/clj/user. It seems I had to use the OS to delete the DB files. Is there a simple and reliable way to regenerate the DB?

Making sure my project.clj dependencies, especially ClojureScript, was updated to match the sample code for the shadow section helped get rid of several errors. Switching from Firefox to Chrome and using Web Dev settings under “console” to "enable custom formatters helped too. I often get burned by cached web pages, but I am starting to remember to look to that first when I see unexpected behavior. Can I use any browser or is Chrome required during development?

I am looking forward to the re-frame work. Thanks again, Pete.

1 Like

Glad to hear you got things sorted out!

You can use any browser, but chrome/chromium has a slight edge with support for devtools. My workflow tends to be to get stuff working in chromium and then validate in firefox once things are fairly complete.

With regards to regenerating the DB from the REPL, it largely depends on what went wrong. luminus-migrations does provide rollback and reset functions, but that will only be as good as your migrations are. If you’ve made an error in writing a migration file, it won’t help. I’d recommend using SQL (either from your favourite client or from the REPL) to inspect the structure of your tables and compare against what you expect.

Happy Hacking!

Scot

1 Like