ViperCoderJay

ViperCoderJay

Pull out bulk data from the database using ecto and sending it as json.
Somewhere within the data is an invalid byte 0xFA utf8 code, my code to pull the data

data = Transactions
|> where([a], a.serviceType in [6, 7] and is_nil(a.deleted_at))
|> join(:left, [a], b in User, on: a.transactingUser_id == b.id)
|> select([a], %{
        channel: a.channel,
        plaza: a.startRoute,
        ref: a.transactionRef,
        cashier: fragment("concat(?, concat(' ', ?))", b.firstName, b.lastName)
}
|> Repo.all()

json(conn, data)

How can I solve this problem?
If possible, how can I convert it to string within the ecto query itself?

Showing Posts 1 to 10

kip

kip

ex_cldr Core Team

Please paste code between triple back-ticks (``` before and after) so people have a chance to understand it.

The code point 0xfa is valid unicode:

iex> <<0xfa :: utf8 >> == "ú"
true

So there is something about the sequence of bytes that is making the string be invalid UTF8. That suggests that the encoding of the data in the database is not UTF8.

You could confirm the encoding of your database with:

kip=# show server_encoding;
 server_encoding 
-----------------
 UTF8
(1 row)

It would also be helpful if you can post the complete binary that is invalid so we can investigate further.

NobbZ

NobbZ

The codepoint U+00fa is valid unicode as you have shown, though the problem is the byte.

The byte 0xFA alone is not valid UTF-8, as can be seen in this table:

0xFA is marked red, which means “is not allowed to appear in a UTF-8 encoded sequence of bytes”.

NobbZ

NobbZ

Can you please share the full error? that migh help to understand where it comes from.

Also please share your relevant schema and migrations.

ViperCoderJay

ViperCoderJay OP

(exit) an exception was raised:
     ** (Jason.EncodeError) invalid byte 0xFA in <<77, 105, 108, 100, 114, 101, 100, 32, 67, 104, 105, 110, 103, 250, 109, 98, 97>>
         (jason) lib/jason.ex:199: Jason.encode_to_iodata!/2
         (phoenix) lib/phoenix/controller.ex:284: Phoenix.Controller.json/2
         (nrfa_elixir) lib/elixir_web/UI CONTROLLERS/transactions/transactions_controller.ex:1: ElixirWeb.TransactionsController.action/2
         (nrfa_elixir) lib/elixir_web/UI CONTROLLERS/transactions/transactions_controller.ex:1: ElixirWeb.TransactionsController.phoenix_controller_pipeline/2
         (phoenix) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
         (nrfa_elixir) lib/elixir_web/endpoints/80/endpoint.ex:1: ElixirWeb.Endpoint.plug_builder_call/2
         (nrfa_elixir) lib/elixir_web/endpoints/80/endpoint.ex:1: ElixirWeb.Endpoint.call/2
         (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4

thats the error am giving

ViperCoderJay

ViperCoderJay OP


(exit) an exception was raised:
     ** (Jason.EncodeError) invalid byte 0xFA in <<77, 105, 108, 100, 114, 101, 100, 32, 67, 104, 105, 110, 103, 250, 109, 98, 97>>
         (jason) lib/jason.ex:199: Jason.encode_to_iodata!/2
         (phoenix) lib/phoenix/controller.ex:284: Phoenix.Controller.json/2
         (nrfa_elixir) lib/elixir_web/UI CONTROLLERS/transactions/transactions_controller.ex:1: ElixirWeb.TransactionsController.action/2
         (nrfa_elixir) lib/elixir_web/UI CONTROLLERS/transactions/transactions_controller.ex:1: ElixirWeb.TransactionsController.phoenix_controller_pipeline/2
         (phoenix) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
         (nrfa_elixir) lib/elixir_web/endpoints/80/endpoint.ex:1: ElixirWeb.Endpoint.plug_builder_call/2
         (nrfa_elixir) lib/elixir_web/endpoints/80/endpoint.ex:1: ElixirWeb.Endpoint.call/2
         (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
NobbZ

NobbZ

There is a byte 250 close to the end. That is the 0xFA from the error.

Please check your database directly what byte there is and fix the data in the database.

The following shows what is exactly in fornt of and after that byte:

iex(1)> <<77, 105, 108, 100, 114, 101, 100, 32, 67, 104, 105, 110, 103>>
"Mildred Ching"
iex(2)> <<109, 98, 97>>
"mba"
ViperCoderJay

ViperCoderJay OP

So how can I detect the error and replace it with a string directly from the database because my app is crashing when it encounters that error, code example please

kip

kip

ex_cldr Core Team

As @NobbZ says, the primary issue here is that you have invalid UTF8-encoded data in your database. Therefore the correct fix is to correct the data in your database.

If you are unable to correct the data in your database then:

  1. Change your ecto schema type to :binary (not :string).
  2. After retrieving from the database do String.chunk(binary, :valid) to split the string into valid and invalid parts.
  3. Decide what policy you want to apply when there is invalid UTF-8 encoding in your data. Possibly something like Enum.filter(chunks, &String.valid?/1) |> Enum.join()
ViperCoderJay

ViperCoderJay OP

Thanks alot.

Can’t manage correcting my database manual

iex(5)> Transactions |> Repo.aggregate(:count, :id)
2466060

Thats the count of transactions I have in the DB. Can’t manage going through it one by one.

thanks

NobbZ

NobbZ

No one said it has to be done manually.

Though in my opinion it has to happen!

Make sure that your database is encoding aware and not just assuming binary/opaque blob of data.

Then write a program that fixes all the malformed data you have according to the policy you have decided to go with.

Keeping malformed data in the DB and reinventing fixes for each consumer of the database is what made us see all the questionmarks and backslashes in the early 2000s, as so many PHP folks tried to fix data before pushing to DB and another set of developers tried to fix when pulling out, leading to “double fixed” data which again was often worse, as the roundtrip of fixing started again with each data edit.

On top of that, developers just didn’t understand all the levels of escaping..

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews