V
V
vadimstroganov2015-10-22 11:55:46
PostgreSQL
vadimstroganov, 2015-10-22 11:55:46

How to catch postgres error?

Hello! Is there any way to catch the postgres error

ActiveRecord::StatementInvalid (PG::CharacterNotInRepertoire: ERROR:  invalid byte sequence for encoding "UTF8": 0xdf 0xd4

and if it happened, render your response in json?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Pyankov, 2015-10-22
@ibub1ik

IMHO, errors at the DB level should not fall. The database is the last frontier, and the occurrence of errors at its level is a miscalculation in the application logic. Why not use validations?

R
Roman Mirilaczvili, 2015-10-22
@2ord

In the controller method:

begin
  # здесь находится AR запрос ...
rescue ActiveRecord::StatementInvalid => e
  render( json: { message: "ActiveRecord::StatementInvalid error: #{e.message}" }, status: :bad_request )
  return
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question