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

gen: Error handler is bypassed when using convenient error feature #1145

Open
danilvpetrov opened this issue Dec 22, 2023 · 0 comments
Open
Labels
bug Something isn't working

Comments

@danilvpetrov
Copy link
Contributor

TL;DR

Not sure if it is a bug or intended behaviour. When convenient errors are enabled and used, the error handler is not called when an endpoint's handler implementation returns an error.

What version of ogen are you using?

$ go list -m github.com/ogen-go/ogen
github.com/ogen-go/ogen v0.81.0

Can this issue be reproduced with the latest version?

Yes

What did you do?

  1. Created a temp directory, ran go mod init example.com/test/test in it.
  2. Created a file api.yml with the following content:
openapi: 3.0.2
servers:
  - url: /v3
info:
  version: 1.0.0
  title: Pet store schema
tags:
  - name: pet
    description: Everything about your Pets
paths:
  /pets:
    get:
      tags:
        - pets
      summary: Get all pets
      description: Returns all pets
      operationId: getPets
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                type: object
                required:
                  - error_message
                properties:
                  error_message:
                    description: A description of the error
                    type: string

components:
  schemas:
    Pet:
      type: object
      required:
        - name
      properties:
        id:
          type: integer
          format: int64
          example: 10
        name:
          type: string
          example: doggie
        photoUrls:
          type: array
          items:
            type: string
  1. Ran the following command to generate code: ogen --target gen --package gen --clean api.yml in the temporary directory.
  2. Created a main.go file in the temporary directory with the following content:
package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"

	"example.com/test/test/gen"
)

type handler struct{}

func (h *handler) GetPets(_ context.Context) ([]gen.Pet, error) {
	return nil, errors.New("get pets error")
}

func (h *handler) NewError(_ context.Context, err error) *gen.ErrRespStatusCode {
	return &gen.ErrRespStatusCode{
		StatusCode: http.StatusInternalServerError,
		Response: gen.ErrResp{
			ErrorMessage: "internal server error",
		},
	}
}

func main() {
	s, err := gen.NewServer(
		&handler{},
		gen.WithErrorHandler(func(
			ctx context.Context,
			w http.ResponseWriter,
			r *http.Request,
			err error,
		) {
			fmt.Printf("trying to log this error: %v\n", err)
		}),
	)
	if err != nil {
		panic(err)
	}

	http.ListenAndServe(":8080", s)
}
  1. Ran go run main.go in the temporary directory.
  2. Ran curl localhost:8080/pets In a separate terminal window

What did you expect to see?

The expectation was to see a print out trying to log this error: get pets error In the terminal window that was running go run main.go command.

What did you see instead?

No print out of the expected message was seen.

@danilvpetrov danilvpetrov added the bug Something isn't working label Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant