Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTFul About 404 error handling #14775

Open
xutl opened this issue Sep 7, 2017 · 8 comments
Open

RESTFul About 404 error handling #14775

xutl opened this issue Sep 7, 2017 · 8 comments
Labels
type:docs Documentation

Comments

@xutl
Copy link
Contributor

xutl commented Sep 7, 2017

What steps will reproduce the problem?

  1. open restful url http://localhost/api/users/1
    return 200 ok
  2. open restful url http://localhost/api/users/111111111
    return 404 this is normal
  3. When opening a non-existent URL e.g http://localhost/api/abcdefg
    Returned the html 404 page,He should return the same format as xml or json as in the second step.

What is the expected result?

Returns a formatted xml or json.

What do you get instead?

Additional info

Q A
Yii version 2.0.12
PHP version 7.1.6
Operating system centos 7.1
@schmunk42
Copy link
Contributor

Do you have an ApiController or a UsersController in an api module? If the latter your app might not know about the fact that you wanted an API page, when you're serving mixed content.

@samdark
Copy link
Member

samdark commented Sep 7, 2017

As @schmunk42 said, in order to respond 404 in this case, custom error handler is needed. I think it's a good idea to document it. Pretty standard use case.

@samdark samdark added the type:docs Documentation label Sep 7, 2017
@samdark samdark added this to the 2.0.16 milestone Sep 7, 2017
@xutl xutl closed this as completed Sep 8, 2017
@dmirogin
Copy link
Contributor

dmirogin commented Sep 8, 2017

This usecase has not documented yet.

@dmirogin dmirogin reopened this Sep 8, 2017
@pgyf
Copy link

pgyf commented Sep 11, 2017

it is handled in this action

        'errorHandler' => [
            'errorAction' => 'site/error',
        ],

@sunjayaali
Copy link

@phpyii have you try in rest application?
@samdark so you mean custom error handler is not available until 2.0.16?

@cebe
Copy link
Member

cebe commented Sep 18, 2017

custom error handler does already work, the only thing that is missing in this issue is documentation.

@xutl xutl closed this as completed Oct 14, 2017
@samdark samdark reopened this Oct 14, 2017
@jurchiks
Copy link

I've tried all kinds of "solutions" for this problem and none of them work for me.
My current setup:

  • yii2-advanced template
  • backend contains a module called api:
'modules' => [
    'api' => [
        'class' => backend\modules\api\Module::class,
    ],
],
  • this module MUST always respond with JSON, even if a route is not found (which is to be expected from a JSON API)

All existing actions work perfectly fine.
However, when I make a request to a non-existing action in this module (e.g. /api/index/no-such-action), I get the default site/error HTML page! It even ignores the Accept header, which is set to application/json.
Module#init() is not even called, thus anything I try to attempt to do there or in the controller is useless.

How am I supposed to work around this? I don't see a non-hackish way to fix this.

@yii-bot
Copy link

yii-bot commented Apr 13, 2018

Issue moved to yiisoft/yii-api#13

@yii-bot yii-bot closed this as completed Apr 13, 2018
@samdark samdark reopened this Apr 13, 2018
@samdark samdark removed this from the 2.0.17 milestone Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

No branches or pull requests

9 participants