Skip to content

ADD error catching - #109

Open
ninovanhooff wants to merge 10 commits into
samdze:devfrom
ninovanhooff:error-catching
Open

ADD error catching#109
ninovanhooff wants to merge 10 commits into
samdze:devfrom
ninovanhooff:error-catching

Conversation

@ninovanhooff

@ninovanhooff ninovanhooff commented Mar 9, 2025

Copy link
Copy Markdown
Collaborator

What is it

A proc that will execute a provided lambda and catch any errors. The exception is printed to the console.
Optionally, the error is marked as fatal, causing the simulator to be paused and the error message to be printed in red.

This is manly intended to add default error catching to the main update loop, but it can also be used in game code if desired. Sample usage is added

Why

  • I've been using this in Wheelsprung and it has been very helpful to me. At the same time this was super challenging to get working when you have no experience with Nim.
  • Lowers the barrier of entry for transitioning from Lua to Nim as this provides behavior that is provided by the lua sdk but absent from a default C setup

Challenges

  • I wasn't sure how to wrap both procs that have a return type and procs without return type in a single wrapper, so I made 2
  • I wanted to wrap the handler call in the initSdk macro too but that is executed very early in the boot sequence and I couldn't figure that out

Out of scope

I plan a follow-up PR where the error is printed to the screen (device only, since simulator has the Console view). This provides a similar crash experience to the lua sdk and allows players to share a photo of the stacktrace with the dev, as is common practice for lua games.

@ninovanhooff
ninovanhooff requested a review from Nycto March 9, 2025 10:15
Comment thread playdate_example/src/playdate_example.nim Outdated
Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
@ninovanhooff

Copy link
Copy Markdown
Collaborator Author

@Nycto This one is a bit rough still, see my comments.

I thought I'd first make a PR to see if you'd agree to this idea before polishing it.

Note there is also a question for you in there regarding a possibility to have a single proc for both typed and untyped usage.

Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
Comment thread src/playdate/system.nim Outdated
@ninovanhooff
ninovanhooff requested a review from Nycto April 25, 2025 23:00
with:
nim-version: ${{ matrix.nim-version }}
- run: nimble test
- run: nimble test --warning[BareExcept]:off

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/t_system.nim
assert(result == 0)

test "runCatching untyped with exception":
if runnable:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect this to be run on the headless test? the whole sparsemap suite and System APi suite seems to be missing in the logs https://github.com/samdze/playdate-nim/actions/runs/14674842933/job/41189152102?pr=109

Comment thread src/playdate/system.nim
except Exception as e:
logException(e, fatal, messagePrefix)
when compiles(result): # result is only defined if body is a proc that has a return type
result # default value for inferred return type

@ninovanhooff ninovanhooff Apr 25, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default(body) does not work here.
Also, return must not be used in a template

Comment thread src/playdate/system.nim
## if fatal is true, the game will pause
try:
body()
except Exception as e:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while running tests, this line spams BareExcept warnings

@AmorBielyi

Copy link
Copy Markdown

@samdze

Hello Samuele!

My name is Roman, I'm a Golang software engineer, and in my spare time I'm developing this project, I think it could be interesing for you, because you are author of Nim bindings for Playdate, which is great, thank you for this work !!!

Finally, Playdate meets the Golang programming language!

If you’re not familiar with Golang: it’s a modern, high-performance language developed by Google powering tools like Docker, Kubernetes, and many others. And now, it’s coming to the Playdate platform!

Thanks for attention, and sorry in advance for disturbing you.

In case if you are interested:

Main regularly updated PdGo devlog (Playdate Development Forum): https://devforum.play.date/t/golang-support-for-playdate-compiler-sdk-bindings-tools-and-examples/24919

Project repository: https://github.com/playdate-go/pdgo

Project internals #1: https://github.com/playdate-go/pdgo#internals

Project internals #2: https://github.com/playdate-go/pdgo?tab=readme-ov-file#why-not-go-but-tinygo

gopher-on-playdate

@Nycto

Nycto commented Jan 27, 2026

Copy link
Copy Markdown
Collaborator

Hi @AmorBielyi

Thanks for sharing. For clarity though, this project is focused specifically on Nim, and the Go tooling you’re referring to is unrelated at both the language and implementation level. I may be missing something, but I don’t see any opportunity for overlap. But I do wish you good luck with your project; It’s always good to see the community grow.

In the future, though, please open a new issue instead of co-opting an unrelated pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants