-
Notifications
You must be signed in to change notification settings - Fork 131
Add JSON mode for error responses #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
…f 'Query parameter' Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds JSON mode support to Scotty, allowing error responses to be returned as JSON instead of HTML when the jsonMode flag is enabled in Options.
- Added a runtime
jsonModeflag toOptions(defaults toFalse) - Updated all error handlers to return JSON-formatted responses when JSON mode is enabled
- Fixed the error message for
FormFieldNotFoundto say "Form field" instead of "Query parameter" - Fixed a build issue by changing
HashMaptoMapfor Form construction
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| Web/Scotty/Internal/Types.hs | Added jsonMode field to Options data type with default value False |
| Web/Scotty/Trans.hs | Updated unhandledExceptionHandler and notFoundApp to support JSON responses; added NamedFieldPuns extension |
| Web/Scotty/Action.hs | Updated scottyExceptionHandler and someExceptionHandler to support JSON responses; fixed HashMap to Map for Form construction; fixed FormFieldNotFound error message |
| scotty.cabal | Added containers dependency for Map support |
| test/Web/ScottySpec.hs | Added comprehensive tests for JSON mode including 404, 500, and 400 error scenarios |
| examples/json_mode.hs | Added example demonstrating JSON mode usage with toggle between HTML and JSON modes |
| examples/scotty-examples.cabal | Added scotty-json-mode executable configuration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
scotty.cabal
Outdated
| blaze-builder >= 0.3.3.0 && < 0.5, | ||
| bytestring >= 0.10.0.2 , | ||
| case-insensitive >= 1.0.0.1 && < 1.3, | ||
| containers, |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The containers dependency should include a version constraint for consistency with other dependencies in this file. Most dependencies specify version bounds to ensure build reproducibility and avoid unexpected breakage from incompatible versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 89f49ff. Added version constraint: containers >= 0.5 && < 0.8
double check that we add utf-8 encoding to all json responses Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
ocramz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please add tests that show how custom error handlers affect (or don't) the returned HTTP status.
E.g, a user has previously noted:
"a custom handler will lose the raised status, e.g. when lacking a body jsonData will result in a 400, but when installing a custom handler I get a 500."
scotty.cabal
Outdated
| blaze-builder >= 0.3.3.0 && < 0.5, | ||
| bytestring >= 0.10.0.2 , | ||
| case-insensitive >= 1.0.0.1 && < 1.3, | ||
| containers, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please fix
Web/Scotty/Trans.hs
Outdated
| @@ -1,4 +1,4 @@ | |||
| {-# LANGUAGE OverloadedStrings, RankNTypes #-} | |||
| {-# LANGUAGE OverloadedStrings, RankNTypes, NamedFieldPuns #-} | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot don't use NamedFieldPuns
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 89f49ff. Removed NamedFieldPuns and updated code to use explicit field access: jsonMode opts instead of pattern matching.
…tent-Type consistency Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Added tests in commit c222284. The tests demonstrate the current behavior where custom handlers catch exceptions before Added 4 tests showing:
All 120 tests pass. |
ocramz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot add a changelog entry under 'next' describing the changes
Co-authored-by: ocramz <5902760+ocramz@users.noreply.github.com>
Added changelog entry in commit 2e2bb1a. The entry describes the JSON mode feature under the "next" section, including:
|
JSON Mode Implementation - Complete
This PR implements JSON mode for Scotty, allowing all default error responses to be returned as JSON instead of HTML when the
jsonModeflag is enabled inOptions.Changes Made
jsonModeflag toOptions(defaults toFalse)scottyExceptionHandler- Scotty-specific exceptions (400, 413, 422, 500)someExceptionHandler- uncaught exceptions (500)notFoundApp- 404 errorsunhandledExceptionHandler- unhandled ScottyExceptionsFormFieldNotFoundnow says "Form field" not "Query parameter"HashMaptoMapforFormconstruction, addedcontainersdependency with version constraint (>= 0.5 && < 0.8)application/json; charset=utf-8Testing
Usage
scottyOpts (defaultOptions { jsonMode = True }) $ do get "/" $ json $ object ["message" .= "Hello"]All JSON responses use consistent format:
{"status": <code>, "description": "<message>"}withContent-Type: application/json; charset=utf-8.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.