A repository name whose path is longer than 255 characters fails
reference.WithName in the dispatcher. That branch appended a bare
distribution.ErrRepositoryNameInvalid to context.Errors, and since it
is not an errcode.ErrorCoder, errcode.ServeJSON answered 500 UNKNOWN.
Wrap it in errcode.ErrorCodeNameInvalid, as the Repository() branch
right below already does for the same error type.
Since a811c1bb moved the automatic error response into a deferred call
at the top of the dispatcher, the early returns for an invalid name, a
failed Repository() call and a failed repository middleware have each
written the error envelope twice: net/http drops the second WriteHeader
as superfluous and the body ends up with two JSON documents. Remove the
explicit ServeJSON calls and leave the response to the deferred handler.
The Repository() switch gains a default case, so an error of any other
type still gets a 500, now with an UNKNOWN entry instead of an empty
envelope.
Fixes#4699
Assisted-by: Claude Opus 5.5 (via Claude Code)
Signed-off-by: somaz <genius5711@gmail.com>