Search in sources :

Example 1 with Tuple

use of org.rx.bean.Tuple in project rxlib by RockyLOMO.

the class SystemException method setFriendlyMessage.

private void setFriendlyMessage(Map<String, Object> methodSettings, String messageName, ErrorCode errorCode, Object[] messageValues) {
    if (!App.isNullOrEmpty(errorCode.value())) {
        messageName += "[" + errorCode.value() + "]";
    }
    if (!Exception.class.equals(errorCode.cause())) {
        messageName += "<" + errorCode.cause().getSimpleName() + ">";
    }
    String msg = as(methodSettings.get(messageName), String.class);
    if (msg == null) {
        Logger.debug("SystemException: Not found messageName %s", messageName);
        return;
    }
    switch(errorCode.messageFormatter()) {
        case StringFormat:
            msg = String.format(msg, messageValues);
            break;
        case MessageFormat:
            msg = MessageFormat.format(msg, messageValues);
            break;
        default:
            String[] messageKeys = errorCode.messageKeys();
            if (messageKeys.length != messageValues.length) {
                Logger.debug("SystemException: MessageKeys length %s not equals messageValues length %s", messageKeys.length, messageValues.length);
                return;
            }
            NQuery<Tuple<String, Object>> query = NQuery.of(messageKeys).select((p, i) -> Tuple.of(p, messageValues[i]));
            if (data != null) {
                query = query.union(NQuery.of(data.entrySet()).select(p -> Tuple.of(p.getKey(), p.getValue())).toList());
            }
            StringBuilder temp = new StringBuilder().append(msg);
            for (Tuple<String, Object> tuple : query.orderByDescending(p -> p.left.length())) {
                temp.replace(tuple.left, toJsonString(tuple.right));
            }
            msg = temp.toString();
            break;
    }
    friendlyMessage = msg;
}
Also used : NestedRuntimeException(org.springframework.core.NestedRuntimeException) AccessibleObject(java.lang.reflect.AccessibleObject) Contract.toJsonString(org.rx.Contract.toJsonString) BiTuple(org.rx.bean.BiTuple) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Tuple(org.rx.bean.Tuple) MessageFormat(java.text.MessageFormat) WeakCache(org.rx.cache.WeakCache) StringBuilder(org.rx.util.StringBuilder) Const(org.rx.bean.Const) Map(java.util.Map) Contract(org.rx.Contract) Method(java.lang.reflect.Method) StringBuilder(org.rx.util.StringBuilder) AccessibleObject(java.lang.reflect.AccessibleObject) Contract.toJsonString(org.rx.Contract.toJsonString) NestedRuntimeException(org.springframework.core.NestedRuntimeException) BiTuple(org.rx.bean.BiTuple) Tuple(org.rx.bean.Tuple)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 MessageFormat (java.text.MessageFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Contract (org.rx.Contract)1 Contract.toJsonString (org.rx.Contract.toJsonString)1 BiTuple (org.rx.bean.BiTuple)1 Const (org.rx.bean.Const)1 Tuple (org.rx.bean.Tuple)1 WeakCache (org.rx.cache.WeakCache)1 StringBuilder (org.rx.util.StringBuilder)1 NestedRuntimeException (org.springframework.core.NestedRuntimeException)1