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

DefaultConversionService throws confusing NoSuchElementException when converting invalid Enums #403

Closed
lupiter opened this issue May 10, 2018 · 1 comment
Assignees

Comments

@lupiter
Copy link

lupiter commented May 10, 2018

Setting up Locations to convert a parameter to an Enum, as follows:

enum class State{
    ACTIVE,
    UNKNOWN
}

object Paths {
    @Location("/") class getBase(val `state`: List<State>? = null)
}

If request contains a value not in the Enum e.g. /?state=NO, or is empty /?state= then DefaultConversionService throws a NoSuchElementException because the call type.enumConstants.first fails to find a value.

Since the state is nullable it would be good if it could return null in the empty case. In the case of a wrong value it would be helpful if the generic NoSuchElementException could be wrapped in a DataConversionException which could be caught and return 400. A NoSuchElementException could have a variety of causes and would be a very generic catch.

@lupiter lupiter changed the title ConversionService throws confusing NoSuchElementException when converting invalid Enums DefaultConversionService throws confusing NoSuchElementException when converting invalid Enums May 10, 2018
@cy6erGn0m cy6erGn0m self-assigned this May 10, 2018
cy6erGn0m pushed a commit that referenced this issue May 10, 2018
throw specific DataConversionException instead of
 meaningless NoSuchElementException
@cy6erGn0m
Copy link
Contributor

Not sure why empty values should be silently ignored? we only do like that only for missing values while empty values are handled as is

schleinzer pushed a commit to schleinzer/ktor that referenced this issue Feb 26, 2019
throw specific DataConversionException instead of
 meaningless NoSuchElementException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants