ResourceBase { protected <T> T handleExceptions(Supplier<T> response, BiFunction<Integer, ErrorResponse, T> error) { try { return response.get(); } catch (IllegalArgumentException e) { return error.apply(400, new ErrorResponse(e.getMessage())); } catch (NflowNotFoundException e) { return error.apply(404, new ErrorResponse(e.getMessage())); } catch (Throwable t) { return error.apply(500, new ErrorResponse(t.getMessage())); } }  List<ListWorkflowDefinitionResponse> listWorkflowDefinitions(final List<String> types,
      final WorkflowDefinitionService workflowDefinitions, final ListWorkflowDefinitionConverter converter,
      final WorkflowDefinitionDao workflowDefinitionDao); boolean updateWorkflowInstance(final long id,
      final UpdateWorkflowInstanceRequest req, final WorkflowInstanceFactory workflowInstanceFactory,
      final WorkflowInstanceService workflowInstances, WorkflowInstanceDao workflowInstanceDao); Stream<ListWorkflowInstanceResponse> listWorkflowInstances(final List<Long> ids, final List<String> types,
      final Long parentWorkflowId, final Long parentActionId, final List<String> states,
      final List<WorkflowInstanceStatus> statuses, final String businessKey, final String externalId, final String include,
      final Long maxResults, final Long maxActions, final WorkflowInstanceService workflowInstances,
      final ListWorkflowInstanceConverter listWorkflowConverter); ListWorkflowInstanceResponse fetchWorkflowInstance(final long id, final String include, final Long maxActions,
      final WorkflowInstanceService workflowInstances,
      final ListWorkflowInstanceConverter listWorkflowConverter);  }
@Test public void handleExceptionsReturnsResponseWhenSuccessful() { String response = resource.handleExceptions(() -> "ok", this::toError); assertThat(response, is("ok")); }
@Test public void handleExceptionsReturnsBadRequestForIllagelStateException() { String response = resource.handleExceptions(() -> { throw new IllegalArgumentException("error"); }, this::toError); assertThat(response, is("400 error")); }
@Test public void handleExceptionsReturnsNotFoundForNflowNotFoundException() { String response = resource.handleExceptions(() -> { throw new NflowNotFoundException("Item", 1, new Exception()); }, this::toError); assertThat(response, is("404 Item 1 not found")); }
@Test public void handleExceptionsReturnsInternalServerErrorForOtherThrowables() { String response = resource.handleExceptions(() -> { throw new RuntimeException("error"); }, this::toError); assertThat(response, is("500 error")); }