Open slogsdon opened 10 years ago
Add model documentation for #44.
Update docs for rendering for #34.
Be sure to cover testing all aspects of a Sugar-based application. See #57.
When I follow the documentation, I can get the templates rendering, but all attempts at json get this error:
2015-04-24 14:59:15.883 [error] Ranch listener PaperHat.Router.HTTP had connection
process started with :cowboy_protocol:start_link/4 at #PID<0.263.0> exit with reason:
{{%Poison.EncodeError{message: nil, value: {:message, "foobar"}},
[{Poison.Encoder.Any, :encode, 2, [file: 'lib/poison/encoder.ex', line: 213]},
{Poison.Encoder.List, :encode, 2, [file: 'lib/poison/encoder.ex', line: 169]},
{Poison, :encode!, 2, [file: 'lib/poison.ex', line: 41]},
{Sugar.Controller.Helpers, :json, 3, [file: 'lib/sugar/controller/helpers.ex', line: 132]},
{PaperHat.Controllers.Main, :call, 2, [file: 'lib/paper_hat/controllers/main.ex', line: 1]},
{PaperHat.Router, :do_call, 2, [file: 'lib/paper_hat/router.ex', line: 1]},
{Plug.Adapters.Cowboy.Handler, :upgrade, 4,
[file: 'lib/plug/adapters/cowboy/handler.ex', line: 15]},
{:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}]},
{PaperHat.Router, :call, [%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...},
assigns: %{}, before_send: [], cookies: %Plug.Conn.Unfetched{aspect: :cookies},
halted: false, host: "localhost", method: "GET", owner: #PID<0.263.0>,
params: %Plug.Conn.Unfetched{aspect: :params}, path_info: ["json"],
peer: {{127, 0, 0, 1}, 56202}, port: 4000, private: %{}, query_string: "",
remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
req_headers: [{"host", "localhost:4000"}, {"connection", "keep-alive"},
{"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
{"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"},
{"dnt", "1"}, {"accept-encoding", "gzip,deflate,sdch"},
{"accept-language", "en-US,en;q=0.8,es;q=0.6"},
{"cookie", "_ga=GA1.1.476198168.1421808488"}], resp_body: nil, resp_cookies: %{},
resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}],
scheme: :http, script_name: [], secret_key_base: nil, state: :unset, status: nil}, []]}}
which, as far as I can tell, tells me nothing. Is this a documentation issue? Here is my controller:
defmodule PaperHat.Controllers.Main do
use Sugar.Controller
def index(conn, _args) do
conn |> render
end
def show(conn, args) do
conn |> render(args)
end
def get_json(conn, []) do
json conn, [ message: "foobar" ]
end
end
Index and show work fine. Get_JSON, not so much. Ideas?
Here's the config:
use Mix.Config
config :paper_hat, PaperHat.Repos.Main,
database: "paper_hat",
username: "***",
password: "***",
hostname: "localhost"
config :sugar,
router: PaperHat.Router
config :sugar, PaperHat.Router,
https_only: false,
http: [ port: 4000 ],
https: false
config :logger, :console,
level: :info,
format: "$date $time [$level] $metadata$message\n",
metadata: [:user_id]
@chasm It looks like the Poison encoder is what is throwing the error. By default, Poison doesn't include a definition for tuples in its Poison.Encoder
protocol, and since Keyword lists are lists of tuple pairs, Poison doesn't know what to do.
An easy fix for this should be to change your Keyword list to a Map. A more involved fix would be to implement the protocol for two-element tuples.
I'm mobile right now, so I apologize for the lack of examples. Let me know if this doesn't work for you. and I'll follow up again with some concrete fixes.
That worked. Changed docs to reflect it and sent a pull request on the doc repo.
Thanks!
Great! Let us know if you run into any other issues or if you have any suggestions.
Happy to!
It's a wee bit out of date