Invoking custom errors in script#

Blitz Identity Provider allows you to create custom errors and call them in login procedures. Do the following:

  1. Following the instructions, add a custom error message to the messages file in the /usr/share/identityblitz/blitz-config/custom_messages directory.

    err.bad_gateway=Недоступно
    
  2. Call this error upon getting HTTP 502.

    if (result.status() == 502) {
                            return HttpLoop.error("bad_gateway",
                                            Collections.<String, String>singletonMap("status", "" + result.status()));
    }
    

    Sample script that calls a custom HTTP 502 error for the Flash Call authentication method:

    package flashcall;
    
    import com.identityblitz.core.loop.http.HttpLoop;
    import com.identityblitz.core.loop.http.HttpLoopRequest;
    import com.identityblitz.core.loop.http.HttpLoopResult;
    import com.identityblitz.core.loop.JsObj;
    import java.util.Collections;
    
    public class FlashCallFlow implements HttpLoop {
    
            public HttpLoopRequest run(final JsObj obj, final HttpLoopResult result) {
                    if (result == null) {
                            final String number = obj.asString("phone_number");
                            return HttpLoop.callBuilder("POST", "http://test.flashcall.ru/api/v1")
                                            .withHeader("Token", "1234567890")
                                            .withBody(JsObj.empty.addString("id", "test").addString("dst_number", number.substring(number.length() - 10)))
                                            .build(JsObj.empty);
                    } else if (result.status() == 200) {
                            final JsObj body = result.body();
                            return HttpLoop.Ok(JsObj.empty.addString("code", body.asString("SenderID")));
                    } else if (result.status() == 502) {
                            return HttpLoop.error("bad_gateway",
                                            Collections.<String, String>singletonMap("status", "" + result.status()));
                    } else {
                            return HttpLoop.error("wrong_http_status",
                                            Collections.<String, String>singletonMap("status", "" + result.status()));
                    }
            }
    }