Best practice to look up Java Enum

前端 未结 10 958
甜味超标
甜味超标 2021-02-01 13:17

We have a REST API where clients can supply parameters representing values defined on the server in Java Enums.

So we can provide a descriptive error, we add this

10条回答
  •  野性不改
    2021-02-01 13:41

    Looks like you have a bad practice here but not where you think.

    Catching an IllegalArgumentException to rethrow another RuntimeException with a clearer message might look like a good idea but it is not. Because it means you care about messages in your exceptions.

    If you care about messages in your exceptions, then it means that your user is somehow seeing your exceptions. This is bad.

    If you want to provide an explicit error message to your user, you should check the validity of the enum value when parsing user input and send the appropriate error message in the response if user input is incorrect.

    Something like:

    // This code uses pure fantasy, you are warned!
    class MyApi
    {
        // Return the 24-hour from a 12-hour and AM/PM
    
        void getHour24(Request request, Response response)
        {
            // validate user input
            int nTime12 = 1;
            try
            {
                nTime12 = Integer.parseInt(request.getParam("hour12"));
                if( nTime12 <= 0 || nTime12 > 12 )
                {
                    throw new NumberFormatException();
                }
            }
            catch( NumberFormatException e )
            {
                response.setCode(400); // Bad request
                response.setContent("time12 must be an integer between 1 and 12");
                return;
            }
    
            AMPM pm = null;
            try
            {
                pm = AMPM.lookup(request.getParam("pm"));
            }
            catch( IllegalArgumentException e )
            {
                response.setCode(400); // Bad request
                response.setContent("pm must be one of " + AMPM.values());
                return;
            }
    
            response.setCode(200);
            switch( pm )
            {
                case AM:
                    response.setContent(nTime12);
                    break;
                case PM:
                    response.setContent(nTime12 + 12);
                    break;
            }
            return;
        }
    }
    

提交回复
热议问题