package org.radarcns.management.web.rest.errors;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Custom, parameterized exception, which can be translated on the client side.
 *
 * <p>For example:
 *
 * <pre>
 * throw new CustomParameterizedException(&quot;myCustomError&quot;, &quot;hello&quot;,
 * &quot;world&quot;);
 * </pre>
 *
 * Can be translated with:
 *
 * <pre>
 * "error.myCustomError" :  "The server says {{param0}} to {{param1}}"
 * </pre></p>
 */
public class CustomParameterizedException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private static final String PARAM = "param";

    private final String message;

    private final Map<String, String> paramMap = new HashMap<>();

    /**
     * Create an error with the given message and parameters. The parameters passed in will go
     * into the parameter with as values, with keys being {@code param0, param1, ...}.
     * @param message the error message
     * @param params the error parameters
     */
    public CustomParameterizedException(String message, String... params) {
        super(message);
        this.message = message;
        // add default timestamp first, so a timestamp key in the paramMap will overwrite it
        this.paramMap.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date()));
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                paramMap.put(PARAM + i, params[i]);
            }
        }
    }

    /**
     * Create an error with the given message and parameter map.
     * @param message the error message
     * @param paramMap the parameter map
     */
    public CustomParameterizedException(String message, Map<String, String> paramMap) {
        super(message);
        this.message = message;
        // add default timestamp first, so a timestamp key in the paramMap will overwrite it
        this.paramMap.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new Date()));
        this.paramMap.putAll(paramMap);
    }

    public ParameterizedErrorVM getErrorVM() {
        return new ParameterizedErrorVM(message, paramMap);
    }
}

@@ -255,16 +255,21 @@
 example:
+ %3C/p%3E
 %0A *%0A * %3C
@@ -269,22 +269,23 @@
  *%0A * %3Cp
-re%3E%0A *
+%3E%7B@code
  throw n
@@ -316,22 +316,23 @@
 ception(
-&quot;
+%22error.
 myCustom
@@ -340,74 +340,40 @@
 rror
-&quot;, &quot;hello&quot;,%0A * &quot;world&quot;);%0A * 
+%22, %22hello%22, %22world%22)%7D
 %3C/p
-re
 %3E%0A *%0A * 
 Can 
@@ -368,17 +368,20 @@
 %3E%0A *%0A * 
-C
+%3Cp%3Ec
 an be tr
@@ -394,16 +394,20 @@
 ed with:
+%3C/p%3E
 %0A *%0A * %3C
@@ -411,14 +411,15 @@
 * %3Cp
-re%3E%0A *
+%3E%7B@code
  %22er
@@ -438,17 +438,16 @@
 Error%22 :
- 
  %22The se
@@ -485,18 +485,9 @@
 1%7D%7D%22
-%0A * %3C/pre%3E
+%7D
 %3C/p%3E

Fix javadoc