Home Optimising error management on function typed parameter
Reply: 0

Optimising error management on function typed parameter

user1857 Published in July 22, 2018, 8:56 am

I'm having some trouble to correctly manage my errors on my webservice. They are working quite well, but I'd like to have a centralised error management which will allow to easily control the status/string returned.

For that, I have creater a parent class for all my webservices. Here is an example of the getList. This function allows to retrieve a list of Object JSONised. It also manages the http status.

public ResponseEntity<String> getList(Function getFunction) {
    String retour = error;
    HttpStatus statusRetour = HttpStatus.INTERNAL_SERVER_ERROR;

    try {
        Object ret = getFunction.apply(null);
        if (ret == null || ((List) ret).size() == 0) {
            statusRetour = HttpStatus.NOT_FOUND;
        } else {
            retour = parser.toJson(((List) ret).stream().map(s -> parser.toJson(s)).collect(Collectors.toList()));
            statusRetour = HttpStatus.OK;
    } catch (Exception e) {
        Logger.error(getFunction.toString(), e);
    return new ResponseEntity<String>(retour, statusRetour);

return this.getList(this.getUserFactory()::getAllUsers);

This is working quite well. My problems is, I can't give it a function that trows Exception as a paramter with a "::" style, even if I have managed them in my function which gives me this kind of thing...

return this.getList(t -> {
        try {
            return this.getMetierFactory().getAllMetier(t);
        } catch (Exception e) {
            // some error management ?
            return null;

And I find this kinda ugly :p

Would you have any idea ? I really love the "::" syntax. It's way clearer to read and way more logic as I want my error management to be realised in my getList function.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.296862 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO