logo
down
shadow

How to raise custom Postgresql error and handle it in Ecto


How to raise custom Postgresql error and handle it in Ecto

Content Index :

How to raise custom Postgresql error and handle it in Ecto
Tag : postgresql , By : Trevor Cortez
Date : November 25 2020, 07:22 PM

I wish did fix the issue. As far as I know, there is no built-in mechanism for handling custom PostgreSQL's errors. However, you can do it at the repository level.
To do that, you have to raise errors in the PostgreSQL using ERRCODE like:
RAISE '[message for logs]' USING ERRCODE = 'integrity_constraint_violation';
defmodule Core.Repo do
  use Ecto.Repo, otp_app: :core

  defoverridable insert: 2

  def insert(changeset, opts) do
    super(changeset, opts)
  rescue
    exception in Postgrex.Error ->
      handle_postgrex_exception(exception, __STACKTRACE__, changeset)
  end

  # ... other functions

  defp handle_postgrex_exception(exception, stacktrace, changeset \\ nil)

  defp handle_postgrex_exception(%{postgres: %{code: :integrity_constraint_violation}}, _, nil) do
    {:error, :integrity_constraint_violation}
  end

  defp handle_postgrex_exception(
         %{postgres: %{code: :integrity_constraint_violation}},
         _,
         changeset
       ) do
    {:error, %{changeset | valid?: false}}
  end

  defp handle_postgrex_exception(exception, stacktrace, _) do
    reraise(exception, stacktrace)
  end
end

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

PostgreSQL: Error :too many parameter specified for raise


Tag : postgresql , By : Tony Siu
Date : March 29 2020, 07:55 AM
this one helps. I am trying to run my stored procedure scripts . There are 21 scripts amongst them 18 successfully executed but 3 of them are not executing. One of the script which is failing is given below (I am using PostgreSQL 9.5) , Raise Notice takes only two parameters
What you want probably is:
RAISE NOTICE '%%%', SQLSTATE || ' - ' || SQLERRM;

Error ERROR (undefined_table) in a full text search with Ecto and PostgreSQL


Tag : postgresql , By : Jody Bannon
Date : March 29 2020, 07:55 AM
will help you You can't use u.name like that. u is just a name that exists in Elixir when compiling the query to SQL. It gets renamed while it is compiled. You need to add another ? to fragment and pass u.name for that:
def search(query, search_term) do
  from u in query,
    where: fragment("to_tsvector(?) @@ plainto_tsquery(?)", u.name, ^search_term),
    order_by: fragment("ts_rank(to_tsvector(?), plainto_tsquery(?)) DESC", u.name, ^search_term)
end

Error inserting multiple rows into a PostgreSQL Ecto database using a changeset


Tag : elixir , By : Lex Viatkine
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use Ecto.Multi to sequence insertions for a bunch of changesets, and then run it in a transaction. The transaction will make sure that if there's any error in any insertion, the rest of the changes get rolled back.
multi = Enum.reduce(Enum.with_index(product["categories"]), Ecto.Multi.new, fn {productCategory, index}, multi -> 
   changeset = Api.ProductCategory.changeset(%Api.ProductCategory{c_id: productCategory["value"], p_id: productId})
   Ecto.Multi.insert(multi, index, changeset)
end)

case Repo.transaction(multi) do
  {:ok, categories} ->
    # categories here is a map with the index as key and struct as value
    ...
  {:error, failed_operation, failed_value, changes_so_far} ->
    ...
end

error raise when use within GROUP with string_agg in postgresql


Tag : postgresql , By : Terrence Poon
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You need to put the ORDER BY into the function call and remove the WITHIN GROUP part:
string_agg(at.description, ' and ' ORDER BY atrelation.id)  as tag1 

How to use "RAISE INFO, RAISE LOG, RAISE DEBUG” to track log in PostgreSQL function?


Tag : postgresql , By : Pip
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • What is the command line arguments to compile an SQR report using PostgreSQL?
  • Using postgres table description
  • How can I do an IF ELSE statement on POSTGRES JOIN query without joining more than once?
  • Postgres Completely Ignores Configurations set in postgresql.conf file
  • non-interactive password with createdb command
  • How to convert a postgres json list to an all lowercase postgres array
  • How to make a fast pg_trgm DESC (descending)?
  • Docker build permission problem starting PostgreSQL 11 on Debian 8.6 Jessie
  • Calculate the geographic distance between two points using NetTopologySuite with EF Core and postgis
  • Best approach to implement inheritance in a data warehouse based on a postgres database
  • Concurrent select queries split by row ids Vs one query
  • kafka jdbc source connector error in query mode
  • Is this a postgresql bug? Only one row can not query by equal but can query by like
  • how to retrieve results for past n days using postgresql
  • "PostGIS Error: type “geography” does not exist" when using psql
  • AWS DMS | ERROR: could not access file "pglogical": No such file or directory
  • postgresql query takes forever
  • How to interpret psycopg installation error OSX?
  • Upgrading from Spring Boot 1.5 to 2.0 - cannot execute UPDATE in a read-only transaction
  • How to create SELECT STATEMENT that will loop and then create the sql statement?
  • postgresql views + regexp -> optimizer issue?
  • Get error message from error code - PostgreSQL
  • How to sniff PostgreSQL network traffic?
  • SET with SELECT in PostgreSQL not working
  • Store IDs in Redis sorted set and then select value from Postgresql?
  • Strange PostgreSQL index using while using LIMIT..OFFSET
  • Can not install hstore extension to new created schema
  • SQLSTATE and GET STACKED DIAGNOSTICS information in another function?
  • Connect between client docker and postgres server on host
  • pgadmin can't log in after update
  • what is the postgresql query to find the timestamp without time zone
  • Port-foward command to access postgres from outside AKS not working in kubectl due to ampersand
  • Combine rows in postgres
  • How to find data length and index length of particular tables in a postgresql schema?
  • Shell Script To Write PostgreSQL SQL Query Results In To CSV Format
  • DOCKER environment in production
  • TimescaleDB query to select rows where column value changed from previous row
  • Temporary Postgres table gets lost prematurely
  • Partial unique index in postgres in relation to other rows
  • API returns only last row for a nested struct
  • Converting PostGIS table (postgresql) to GeoJSON
  • How to find if the row is part of index?
  • pgAdmin argument formats can't be mixed
  • How to calculate subtotal in postgreSQL
  • Restoring PostgreSQL database without having a dump just the database files
  • Best way to track the progress of a long-running function (from outside) - PostgreSQL 11?
  • Add columns but keep a specific id
  • pg_restore: [archiver] unsupported version (1.13) in file header
  • How to DB query and then check the result
  • Select multiple rows with condition on each columns
  • Postgres date format with Greek day inside
  • Firing Postgres triggers on different table columns
  • Prepared transactions with Postgres 8.4.3 on CentOS
  • OpenStreetMap and Hadoop
  • OSGI & Apache Commons-DBCP Classloading Issue
  • Postgresql full text search in postgresql - japanese, chinese, arabic
  • Strange postgresql behavior
  • PostgreSql XML Text search
  • Can't install psycopg2 (Python2.6 Ubuntu9.10 PostgreSQL8.4.2)
  • Enable encryption on existing database - AWS RDS Postgresql
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com