1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495(* This file is part of Dream, released under the MIT license. See LICENSE.md
for details, or visit https://github.com/aantron/dream.
Copyright 2021 Anton Bachin *)moduleMessage=Dream_pure.MessagemoduleStatus=Dream_pure.Statustypeerror={condition:[|`ResponseofMessage.response|`Stringofstring|`Exnofexn];layer:[|`App|`HTTP|`HTTP2|`TLS|`WebSocket];caused_by:[|`Server|`Client];request:Message.requestoption;response:Message.responseoption;client:stringoption;severity:Log.log_level;will_send_response:bool;}typeerror_handler=error->Message.responseoptionMessage.promise(* This error handler actually *is* a middleware, but it is just one pathway for
reaching the centralized error handler provided by the user, so it is built
into the framework. *)(* TODO The option return value thing is pretty awkward. *)letcatcherror_handlernext_handlerrequest=Lwt.try_bind(fun()->next_handlerrequest)(funresponse->letstatus=Message.statusresponsein(* TODO Overfull hbox. *)ifStatus.is_client_errorstatus||Status.is_server_errorstatusthenbeginletcaused_by,severity=ifStatus.is_client_errorstatusthen`Client,`Warningelse`Server,`Errorinleterror={condition=`Responseresponse;layer=`App;caused_by;request=Somerequest;response=Someresponse;client=Some(Helpers.clientrequest);severity=severity;will_send_response=true;}inerror_handlererrorendelseLwt.returnresponse)(* This exception handler is partially redundant, in that the HTTP-level
handlers will also catch exceptions. However, this handler is able to
capture more relevant context. We leave the HTTP-level handlers for truly
severe protocol-level errors and integration mistakes. *)(funexn->leterror={condition=`Exnexn;layer=`App;caused_by=`Server;request=Somerequest;response=None;client=Some(Helpers.clientrequest);severity=`Error;will_send_response=true;}inerror_handlererror)