Skip to content
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

Adhere consistently to JSON responses #11

Open
ryanmjacobs opened this issue Apr 18, 2019 · 1 comment
Open

Adhere consistently to JSON responses #11

ryanmjacobs opened this issue Apr 18, 2019 · 1 comment

Comments

@ryanmjacobs
Copy link

ryanmjacobs commented Apr 18, 2019

Hi, first of all I just want to say, this project is incredible! I love having a REST API at my fingertips, and I've written some shell scripts to interact with it without having to think about the backend at all, which is fantastic.

I like to pipe the output of my requests to jq, for some pretty JSON printing:
jq output

However, sometimes the response is plaintext, not JSON... (for example, The auction has already ended.). This breaks the JSON parser and causes some annoying corner cases.


I would love to have the response be similar to:

{"error": "The auction has already ended."}

I'm thinking of throwing a PR together this weekend. Let me know if you have any pointers. I'm thinking that simply defining "error" implies an error, otherwise if it's undefined... then no error.

So in any language you could grab the result and run a control structure such as:

const response = ...;
if (response.error) {
  console.log("error: " + response.error);
  return null;
} else {
  console.log("success");
  cosole.log(response);
}

Plaintext error strings that I've noticed:

  • The requested resource could not be found.
  • The auction has already ended.
@ruippeixotog
Copy link
Owner

Hi @ryanmjacobs, I'm glad you like the project! Your idea makes total sense.

If you just need JSON error responses for domain-specific errors (like auction ended or no snipe found) it suffices to replace the string messages in SnipeServer.scala with a new SnipeError(error: String) case class. If you want to handle all server errors (like unexpected throws or wrong URIs) you'll probably want to make use of a custom akka-http exception handler.

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

No branches or pull requests

2 participants