Core: IRI Converter Exception Messages

Created on 22 Aug 2018  路  6Comments  路  Source: api-platform/core

The default IRI Converter uses the symfony router to handle the generation of the IRI values per entity. If there are any issues with IRI generation or lookup by IRI, the exceptions that get thrown are hard to understand/misleading because the errors will be associated with Routing and not with denormalizing an entity or fetching an entity by an IRI.

Here are a few examples of some scenarios that generate errors with the IRI Converter and have confusing error messages:

  1. No item route associated with the type "%s"
  2. No collection route associated with type "%s"
  3. No route matches \"%s\".

You can get errors 1 and 2 if API Platform tries to serialize an entity that does not have a GET item/collection operation defined which is common when working with DTO's. See this Stackoverflow Post

Error 3 can show up if you pass in malformed IRI value for a relationship entity.

POST books
{
    "title": "Some Value"
    "author": "badvalue" // should be an iri formatted like /authors/1
}

I think a possible solution for friendlier error messages would be to catch exceptions thrown in the in the normalizers and give error messages that are closer to the actual problem.

enhancement question

All 6 comments

  1. No item route associated with the type "%s"
  2. No collection route associated with type "%s"

You can get errors 1 and 2 if API Platform tries to serialize an entity that does not have a GET item/collection operation defined which is common when working with DTO's.

Actually the exception messages here sound exactly correct to me.

But I think no. 3 should be fixed :smile:

The problem mentioned in the SO question really is more complicated. I still believe we need to have better support for DTOs / resource representation classes by having a 2-pass process: https://github.com/api-platform/core/issues/2053

@teohhanhui I think the issue I personally had with errors 1 and 2, is that the error made no sense in the context of what was going on.

I had no idea why a route was even trying to be generated for my Model. It wasn't till i learned that API platform uses the symfony router to generate the IRI, did the error kind of make sense. I think if the error included a clause like No item route associated with type "%s" when generating the IRI during serialization, that would be a bit clearer IMHO.

Sure, I guess we could catch the exception and wrap it. :)

Hi, my solution to avoid declaring a get item operation when I don't want any is to decorate the IriConverter and add an attribute to my resource: https://gist.github.com/soyuka/a7a58732792872f000a09f79741042a4

/edit: whooops wrong issue leaving the comment anyway just in case.

Was this page helpful?
0 / 5 - 0 ratings