Commit 8827f55e by Patryk Czarnik

ExceptionUtils i zbieranie messagów

parent e67567ac
......@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import sklep.model.Product;
import sklep.photo.PhotoUtil;
import sklep.repository.ProductRepository;
import sklep.utils.ExceptionUtils;
@Controller
@RequestMapping("/products")
......@@ -80,7 +81,7 @@ public class ProductController {
System.out.println("Obiekt po zapisie: " + product.getProductId() + " " + product.getProductName());
model.addAttribute("saved", true);
} catch (Exception e) {
model.addAttribute("errors", List.of(e));
model.addAttribute("errors", ExceptionUtils.allMessages(e));
}
return "product_form";
......
package sklep.utils;
import java.util.ArrayList;
import java.util.List;
public class ExceptionUtils {
public static List<String> allMessages(Throwable e) {
List<String> messages = new ArrayList<>();
messages.add(oneMessage(e));
Throwable cause = e.getCause();
while(cause != null) {
messages.add(oneMessage(cause));
cause = cause.getCause();
}
return messages;
}
private static String oneMessage(Throwable e) {
return e.getClass().getSimpleName() + ": " + e.getMessage();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment