Actions
Bug #17525
closedUser unfriendly foreign key exception when creating/updating a resource
Difficulty:
Triaged:
Bugzilla link:
Description
Sending a nonexistent id when creating or updating a resource via API ends up with a foreign key exception on foreman instances running on a postgres backend.
Reproducer using hammer:
hammer domain create --dns-id 3283 --name test.org Could not create the domain: ERROR: insert or update on table "domains" violates foreign key constraint "domains_dns_id_fk" DETAIL: Key (dns_id)=(3283) is not present in table "smart_proxies".
Relevant part of the server log:
2016-11-30T08:09:17 8a771c14 [app] [I] Started POST "/api/domains" for fe80::5054:ff:fef5:1b57 at 2016-11-30 08:09:17 +0000 2016-11-30T08:09:17 8a771c14 [app] [I] Processing by Api::V2::DomainsController#create as JSON 2016-11-30T08:09:17 8a771c14 [app] [I] Parameters: {"domain"=>{"name"=>"test.org", "dns_id"=>"3283"}, "apiv"=>"v2"} 2016-11-30T08:09:17 8a771c14 [app] [W] Action failed | PG::ForeignKeyViolation: ERROR: insert or update on table "domains" violates foreign key constraint "domains_dns_id_fk" | DETAIL: Key (dns_id)=(3283) is not present in table "smart_proxies". | ... 2016-11-30T08:09:17 8a771c14 [app] [I] Rendered api/v2/errors/standard_error.json.rabl within api/v2/layouts/error_layout (0.5ms) 2016-11-30T08:09:17 8a771c14 [app] [I] Completed 500 Internal Server Error in 19ms (Views: 1.9ms | ActiveRecord: 2.4ms)
The reproducer uses domains as a simple example, but the problem is present across all resources in the API.
Expected results:
The server should reply with a more user friendly message instead, for example:
Proxy (id: 3283) doesn't exist.
Additional INFO:
I haven't tested the behavior on different database backends.
Actions