public static String parseErrorMessage ( String errorMessage ) { StringBuilder response = new StringBuilder ( ) ; Pattern pattern = Pattern . compile ( ".*?/series/(\\d*?)/default/(\\d*?)/(\\d*?)/.*?" ) ; Matcher matcher = pattern . matcher ( errorMessage ) ; // See if the error message matches the pattern and therefore we can decode it if ( matcher . find ( ) && matcher . groupCount ( ) == ERROR_MSG_GROUP_COUNT ) { int seriesId = Integer . parseInt ( matcher . group ( ERROR_MSG_SERIES ) ) ; int seasonId = Integer . parseInt ( matcher . group ( ERROR_MSG_SEASON ) ) ; int episodeId = Integer . parseInt ( matcher . group ( ERROR_MSG_EPISODE ) ) ; response . append ( "Series Id: " ) . append ( seriesId ) ; response . append ( ", Season: " ) . append ( seasonId ) ; response . append ( ", Episode: " ) . append ( episodeId ) ; response . append ( ": " ) ; if ( episodeId == 0 ) { // We should probably try an scrape season 0/episode 1 response . append ( "Episode seems to be a misnamed pilot episode." ) ; } else if ( episodeId > MAX_EPISODE ) { response . append ( "Episode number seems to be too large." ) ; } else if ( seasonId == 0 && episodeId > 1 ) { response . append ( "This special episode does not exist." ) ; } else if ( errorMessage . toLowerCase ( ) . contains ( ERROR_NOT_ALLOWED_IN_PROLOG ) ) { response . append ( ERROR_RETRIEVE_EPISODE_INFO ) ; } else { response . append ( "Unknown episode error: " ) . append ( errorMessage ) ; } } else // Don't recognise the error format, so just return it { if ( errorMessage . toLowerCase ( ) . contains ( ERROR_NOT_ALLOWED_IN_PROLOG ) ) { response . append ( ERROR_RETRIEVE_EPISODE_INFO ) ; } else { response . append ( "Episode error: " ) . append ( errorMessage ) ; } } return response . toString ( ) ; }
Parse the error message to return a more user friendly message