AUGERClement

AUGERClement

API raise exception instead of returning []

Hello.

I am fairly new to Phoenix/elixir, but I read the doc and couldn’t find what I was looking for.
I am currently working on a basic Phoenix API. After studying a bit the Ecto documentation, I found the lines about Ecto.get and get_by, which helped me adding new endpoints to request the database with another argument than the id.

Now, I am stuck, because when I request some id which isn’t on the API, it raise a error " Ecto.NoResultsError". What I want is a empty data, or nil, so my other program can check if there is content matching the request.

I undestand the Exception came from the Repo.get, so I tried to rewrite my show function in the controller like that :

def show(conn, %{"accession_number" => accession_number}) do
    try do
      dicom = DICOM.get_dicom_an!(accession_number)
      render(conn, "show.json", dicom: dicom)
    rescue
      e in RuntimeError -> IO.puts(e.message)
      render(conn, "show.json", "")
    end
  end

However, I couldn’t get it to work. I also tried to pattern match
with error in the delete function, but I couldn’t make it work.

What is the way to achieve what I want ?

Most Liked

AUGERClement

AUGERClement

Ok, I get the error, it something I’ve looked up.

What I don’t undestand is why it happen in that case, what can I do, and why does the whole work when I use a valid id.

I checked my PostGRES and my id column is of type uuid (autogenerated). My exception also said :

value `"b416bab4-e526-49cd-8109-45aed0baee3"` cannot be dumped to type :binary_id in query:

So, I think I am not using an integer id. What am I missing ?

Where Next?

Popular in Questions Top

mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement