45
Platinum Sponsor EXPECT THE UNEXPECTED Kito D. Mann Principal Consultant

Expect the Unexpected Kito D. Mann Principal Consultant

Embed Size (px)

DESCRIPTION

Kito D. »Principal Consultant at Virtua »http://www.virtua.com »Training, consulting, architecture, mentoring, »JSF product development »Author, JavaServer Faces in Action »Founder, JSF Central »http://www.jsfcentral.com »Internationally recognized speaker »JavaOne, JavaZone, Devoxx, NFJS, TSSJS, etc. Copyright (C) Virtua, Inc. All rights reserved.

Citation preview

Page 1: Expect the Unexpected Kito D. Mann Principal Consultant

Platinum Sponsor

EXPECT THE UNEXPECTEDKito D. Mann

Principal Consultant

Page 2: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Kito D. Mann@kito99

» Principal Consultant at Virtua» http://www.virtua.com» Training, consulting, architecture, mentoring, » JSF product development

» Author, JavaServer Faces in Action» Founder, JSF Central

» http://www.jsfcentral.com» Internationally recognized speaker

» JavaOne, JavaZone, Devoxx, NFJS, TSSJS, etc.

Page 3: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Kito D. Mann@kito99

» JCP Member» JSF, WebBeans, JSF Portlet Bridge, Portlets

Page 4: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

bad things happen

Page 5: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 6: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 7: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

“that should never happen”

Page 8: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 9: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 10: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 11: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

some bad things seem minor

Page 12: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 13: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 14: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

ex·cep·tion

noun. something excepted; an instance or case not conforming to the general rule.

Page 15: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

expect bad things

Page 16: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

regardless of the what is happening, your application should be in a

consistent state.

Page 17: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

exception handling aphorisms

Page 18: Expect the Unexpected Kito D. Mann Principal Consultant
Page 19: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

don't eat exceptions

Page 20: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

public void save() {try {

getWidgetProvider().add(selectedWidget);} catch (DatabaseException e) {

e.printStackTrace();}display("Your widget has been created successfully.");setSelectedWidget(null);

}

Page 21: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

if you can recover from it, catch it and tell the user (if necessary)

Page 22: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

public void save() throws DatabaseException {try {

getWidgetProvider().add(selectedWidget);display("Your widget has been created successfully");setSelectedWidget(null);

} catch (DatabaseException e) {displayError("Sorry, a database error has occurred. Please

try again later.");logger.log(Level.SEVERE, "Error accessing the database",

e);}

}

Page 23: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

if you cannot recover from it, don't catch it

Page 24: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

public void save() throws DatabaseException {getWidgetProvider().add(selectedWidget);display("Your widget has been created successfully");setSelectedWidget(null);

}

Page 25: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

demo

Page 26: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

for any call, if something goes wrong, ensure consistency

Page 27: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

try {session = openSession();if (rule.isCachedModel() || BatchSessionUtil.isEnabled()) {Object staleObject = session.get(RuleImpl.class,rule.getPrimaryKeyObj());

if (staleObject != null) {session.evict(staleObject);}}

session.delete(rule);session.flush();

}catch (Exception e) {

throw processException(e);}finally {

closeSession(session);}

Page 28: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

use a logging framework consistently

Page 29: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

do not log an exception more than one time

Page 30: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

demo

Page 31: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

throw meaningful exceptions

Page 32: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Page 33: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

centralize exception handling

Page 34: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

centralize exception handling

» Logging» Notifications» Error page

Page 35: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

web.xml<web-app … version="3.0">

…<error-page><exception-type>java.lang.Throwable</exception-type><location>/error.jsf</location></error-page>

</web-app>

Page 36: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

JSF 2public class CustomExceptionHandler extends

ExceptionHandlerWrapper {

private static final Logger logger = Logger .getLogger(WidgetViewerBean.class.getName());

private ExceptionHandler wrapped;

public CustomExceptionHandler(ExceptionHandler wrapped) { this.wrapped = wrapped; }

Page 37: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

JSF 2 @Override public void handle() throws FacesException { Iterator<ExceptionQueuedEvent> i =

getUnhandledExceptionQueuedEvents() .iterator(); while (i.hasNext()) { ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event .getSource(); Throwable t = context.getException(); try { logger.log(Level.SEVERE,

"Serious error happened!", t); } finally { i.remove(); } } getWrapped().handle(); }

Page 38: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

JSF 2 @Override public ExceptionHandler getWrapped() { return this.wrapped; }}

Page 39: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

CDI – Apache DeltaSpike

@ExceptionHandlerpublic class MyHandlers {    void logExceptions(@Handles         @WebRequest CaughtException<Throwable> evt,          Logger log) {       log.error("Something bad happened!”, evt.getException());    } }

Page 40: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Spring MVC@Controllerpublic class SimpleController {

@ExceptionHandler(IOException.class) public String handleIOException(

IOException ex, HttpServletRequest request) { logger.error("A " + ex.getClass().getSimpleName() +

" has occured in the application", ex); return "error"; }}

Page 41: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

Spring MVCpublic class SampleExceptionHandler extends

SimpleMappingExceptionResolver {

  private static final Logger logger = LoggerFactory.getLogger(SampleExceptionHandler.class);

  @Override  protected ModelAndView doResolveException(

HttpServletRequest request, HttpServletResponse response, Object handler,

      Exception ex) {    logger.error("A " + ex.getClass().getSimpleName() +

" has occured in the application", ex);    return super.doResolveException(request, response,

handler, ex);  }}

Page 42: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

handle browser exceptions

Page 43: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

handle browser exceptions» in JavaScript code» returned from Ajax requests» while processing Ajax requests

Page 44: Expect the Unexpected Kito D. Mann Principal Consultant

Copyright (C) 2012-14 Virtua, Inc. All rights reserved.

demo

Page 45: Expect the Unexpected Kito D. Mann Principal Consultant

Questions?