Skip to content

Instantly share code, notes, and snippets.

View jordi-chacon's full-sized avatar

Jordi Chacón jordi-chacon

  • Distru
  • Barcelona
View GitHub Profile
@jordi-chacon
jordi-chacon / flatten.ex
Last active January 19, 2019 23:48
Elixir function that flattens an array of integers without using List.flatten
defmodule M do
def flatten(l) do
flatten(l, [])
|> Enum.reverse
end
def flatten([], acc) do
acc
end
def flatten([head | tail], acc) when is_integer(head) do
{
"version": "1.0.0-*",
"webroot": "wwwroot",
"exclude": [
"wwwroot"
],
"packExclude": [
"**.kproj",
"**.user",
"**.vspscc"
When I send a request to my Elixir application which causes an
exception to be thrown from my request handler, *sometimes*,
the server seems to go down. The following error is printed:
#PID<0.613.0> running UserService.Endpoint terminated
Server: localhost:4001 (http)
Request: GET /v1/users/1234
** (exit) an exception was raised:
** (Poison.EncodeError) unable to encode value: {Plug.Adapters.Cowboy.Conn, {:http_req, #Port<0.9678>, :ranch_tcp, :keepalive, #PID<0.613.0>, "GET", :"HTTP/1.1", {{127, 0, 0, 1}, 48857}, "localhost", :undefined, 4001, "/v1/users/1234", :undefined, "", :undefined, [], [{"accept", "application/vnd.api+json"}, {"correlation-id", "1234"}, {"user-agent", "hackney/1.3.1"}, {"host", "localhost:4001"}], [], :undefined, [], :waiting, "", :undefined, false, :done, [], "", :undefined}}
(poison) lib/poison/encoder.ex:339: Poison.Encoder.Any.encode/2
{"timestamp":"2015-08-29 07:28:18.636","service":"facebook_service","message":{"type":"http_request","attributes":{"uri":"/v1/users/1234","query_string":{},"pa
yload":{"foo":"bar"},"method":"PUT","headers":{"user-agent":"hackney/1.3.1","correlation-id":"1234","host":"localhost:4000","content-type":"application/vn
d.api+json","content-length":"13","accept":"application/vnd.api+json"}}},"level":"info","instance":{"id":"","availability_zone":""},"env":"test","correlation_i
d":"1234"}
defmodule FacebookService.Router do
use FacebookService.Web, :router
scope "/v1/users", FacebookService do
get "/", UserIndexController, :index
get "/:user_id", UserShowController, :show
put "/:user_id", UserUpdateController, :update
end
end
@jordi-chacon
jordi-chacon / gist:cf7d67024176b920bff9
Last active August 29, 2015 14:27
System.cmd not returning expected result
~/dev/$ docker ps -a -q --filter "name=build_environment"
b6bc93894233
iex(49)> :os.cmd(String.to_char_list("docker ps -a -q --filter \"name=build_environment\""))
'b6bc93894233\n'
iex(50)> System.cmd("docker", ["ps", "-a", "-q", "--filter", "\"name=build_environment\""])
{"b6bc93894233\n55a77d7dbbd6\n3a5d4c0e317e\ndaed62533b44\na9a21d999a22\nf84b6ecd4744\nd510d0839be6\nd0ad26628d78\nb67fda5e09f1\n02741d8990d7\n5b053bfcd774\n11b791e316db\nb7b37bd15bb1\n2233b1cb2c1e\n660278b07c79\n366dbfc2dcf3\n5cb38e10ac35\n9203065c641e\n9df8469bc205\n07a04ec2631b\nee2f5aa6f994\n38763c699a56\n562491df7082\na84f932fc9d4\n5fc4ac7f9a97\ndf7fcb6c14f9\nae665a3123b7\n9499980a188c\n015fe24ae620\n25eab5269448\n0abd0606a280\n6ba6753a0d3a\nc07b50ba4afa\nc62bb89936ca\naeab78fedc5f\nb05a4a95026b\n01707f07aeb9\n9fa5a9268c1c\n8aeea2438284\n1d3a0f49bb69\n615091e8b1c2\n5e060bba3036\n620c2c799ba9\n0bc40a5c2e53\n363fd37f2ca3\n0a27376d816c\n36c4e7c3fc46\n6db0ace85f83\n077a91d06078\n980df8eb6eb6\nc411756633d2\n24c7e3044f03\n35aed2cc0a47\n7
defmodule MyService.Mixfile do
use Mix.Project
def project do
[app: :my_service,
version: "0.0.4",
elixir: "~> 1.0",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
#!/usr/bin/env elixir
defmodule Script do
def main(_args) do
IO.puts ""
end
end
@jordi-chacon
jordi-chacon / gist:3d408f57b437f89b5218
Last active August 29, 2015 14:21
Exception on 404 handling after calling Phoenix.Controller.json(conn, %{})
error_view.ex
defmodule FacebookService.ErrorView do
use FacebookService.Web, :view
def render("404.json-api", %{conn: conn}) do
conn
|> HTTPex.Plugs.SetContentTypeResponseHeader.call
|> Phoenix.Controller.json(%{})
end
@jordi-chacon
jordi-chacon / gist:48fcc77be034a0dffb16
Created May 25, 2015 06:57
Crash when handling a 404
# error_view.ex
defmodule FacebookService.ErrorView do
use FacebookService.Web, :view
def render("404.json-api", _) do
%{}
end
def render("500.json-api", %{conn: conn}) do