goopenapiopenapi-generator

How to handle SecurityErrors as convenient errors with ogen


I'm using ogen to generate my API server and have enabled convenient errors. I have an API spec which includes a security scheme and I enabled this scheme globally for all endpoints. My question is how I can detect that the error I receive in my convenient error handler is actually a SecurityError, e.g. when no credentials were specified. I tried it with the following code but it seems the error type is lost somewhere:

func (p *Service) NewError(ctx context.Context, err error) (r *api.ErrorStatusCode) {
    var securityError ogenerrors.SecurityError
    if ok := errors.As(err, &securityError); ok {
        // This code is never executed
        log.Println("this is a security error", err)
    }
}

Solution

  • Apparently I was using a value when I should have used a pointer and my IDE did not detect the problem either:

    func (p *Service) NewError(ctx context.Context, err error) (r *api.ErrorStatusCode) {
        var securityError *ogenerrors.SecurityError
        if ok := errors.As(err, &securityError); ok {
            // This works now 
            log.Println("this is a security error", err)
        }
    }