Inconsistencies in the class Nng\Nnrestapi\Mvc\Response

Issue #6 resolved
Karl Berger created an issue

I noticed that calling Response::error doesn’t render a response body correctly. After looking at the class, we can see the following code:

    /**
     * Einen Fehler ausgeben
     *
     * @return void
     */
    public function error( $statusCode, $message = '' ) {
        return $this->setStatus($statusCode)->setMessage($message)->render();
        return [
            'status'    =>$statusCode,
            'error'     =>$message
        ];
    }

With two return statements. That doesn’t make any sense and the response is incorrect.

I would suggest the following fix:

/**
     * Einen Fehler ausgeben
     *
     * @return void
     */
    public function error( $statusCode, $message = '' ) {
        return $this->setStatus($statusCode)->setMessage($message)->render(
        [
            'status'    =>$statusCode,
            'error'     =>$message
        ]);
    }

Which is like any other methods do in this class. BTW I’d also suggest to rename this method to something more generic because it’s actually not related to errors as I could also set a 200 status in there.

Note: there’s the same issue with the method Response::unauthorized

Comments (3)

  1. Log in to comment