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

Map SQL errors to Fuego error: create packages in /extra #399

Open
EwenQuim opened this issue Feb 12, 2025 · 3 comments
Open

Map SQL errors to Fuego error: create packages in /extra #399

EwenQuim opened this issue Feb 12, 2025 · 3 comments
Labels
good first issue Good for newcomers
Milestone

Comments

@EwenQuim
Copy link
Member

Is your feature request related to a problem? Please describe.

Mapping sql errors to fuego errors is so nice, it helps a lot readability.

Describe the solution you'd like

An extra package for sqlite that looks like this

// In `extra` package
func SQLiteErrorHandler(err error) error {
	var sqliteError *sqlite.Error
	if errors.As(err, &sqliteError) {
		sqliteErrorCode := sqliteError.Code()
		switch sqliteErrorCode {
		case 1555, 2067 /* UNIQUE constraint failed */ :
			return fuego.ConflictError{Title: "Duplicate", Detail: sqliteError.Error(), Err: sqliteError}
		default:
			return fuego.InternalServerError{Title: "Internal Server Error", Detail: sqliteError.Error(), Err: sqliteError}
		}
	}

	return err
}

// USAGE
func customErrorHandler(err error) error { 
	return fuego.ErrorHandler(SQLiteErrorHandler(err)) 
}

fuego.NewServer(
  fuego.WithEngineOptions(
    fuego.WithErrorHandler(customErrorHandler),
  )
)

Also please make an extra package for SQL that maps common sql errors like sql.ErrNoRows to fuego.NotFoundError

@EwenQuim EwenQuim changed the title SQL packages in /extra Map SQL errors to Fuego error: create packages in /extra Feb 12, 2025
@EwenQuim EwenQuim added the good first issue Good for newcomers label Feb 12, 2025
@EwenQuim EwenQuim added this to the v0.19 milestone Feb 12, 2025
@dotandev
Copy link

Can I handle this task?

@GideonBature
Copy link

I'd like to handle this task.

@EwenQuim
Copy link
Member Author

You two can take this issue!

I won't assign you to make sure nobody's blocked to create their PR. I'll assign you after you create your draft PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants