public override void OnException(HttpActionExecutedContext actionExecutedContext) { BuildErrorResponse(actionExecutedContext); bool isLoggable = true; Exception baseEx = actionExecutedContext.Exception; ExposeRestException restException = baseEx as ExposeRestException; if (restException != null) { baseEx = restException.InnerException ?? baseEx; // use the base Exception so that we don't get the async stacks added in the trace if (!restException.IsLoggable) { isLoggable = false; var loggingContext = LoggingHelper.GetLoggingContext(); if (loggingContext != null) { loggingContext.LogRequest = isLoggable; } } } if (isLoggable) { // Create log as soon as possible, or it will be done without the errorLogId inside the OnActionExecuted (since that is the normal logging place) string errorLogId = ErrorLog.LogApplicationError(baseEx, AppInfo.GetAppInfo().OsContext, "REST (Expose)"); LoggingHelper.LogResponse(actionExecutedContext, errorLogId); } try { if (actionExecutedContext.ActionContext.ControllerContext != null) { var controller = actionExecutedContext.ActionContext.ControllerContext.Controller; if (controller != null) { var responseFilters = actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes <OnResponseFilter>(); foreach (var filter in responseFilters) { filter.OnActionExecuted(actionExecutedContext); } } } } catch (Exception exceptionInsideExceptionHandler) { ExposeRestException restExceptionInsideExceptionHandler = exceptionInsideExceptionHandler as ExposeRestException; if (restExceptionInsideExceptionHandler != null) { // use the base Exception so that we don't get the async stacks added in the trace exceptionInsideExceptionHandler = restExceptionInsideExceptionHandler.InnerException ?? exceptionInsideExceptionHandler; } // lets log the second error as well, just so it doesn't get lost. The one associated with the Integration Log will be the original one ErrorLog.LogApplicationError(exceptionInsideExceptionHandler, AppInfo.GetAppInfo().OsContext, "REST (Expose)"); // If an exception occurs in the ResponseFilter, build a new error response and don't let it "bubble up" // The new exception will override the old one as the user might change the error message actionExecutedContext.Exception = restExceptionInsideExceptionHandler; BuildErrorResponse(actionExecutedContext); } }
protected void LogApplicationError(Exception ex, HeContext context) { var errorLogId = ErrorLog.LogApplicationError(ex, context, "Service API"); var loggingContext = LoggingHelper.GetLoggingContext(); if (loggingContext != null) { loggingContext.ErrorLogId = errorLogId; } }
protected void TrySetOriginalRequestKey(string requestKey) { if (requestKey != null) { var loggingContext = LoggingHelper.GetLoggingContext(); if (loggingContext != null) { loggingContext.OriginalRequestKey = requestKey; } } }
protected static void SetRequesterLoggingInfo(HeContext heContext) { var loggingContext = LoggingHelper.GetLoggingContext(); if (loggingContext != null && loggingContext.RequesterUserId == 0 && heContext != null && heContext.Session != null && heContext.Session.NewRuntimeLoginInfo != null) { loggingContext.RequesterUserId = heContext.Session.NewRuntimeLoginInfo.UserId; loggingContext.RequesterUsername = heContext.Session.NewRuntimeLoginInfo.Username; loggingContext.RequesterLoginId = heContext.Session.NewRuntimeLoginInfo.LoginId; } }
protected IHttpActionResult endpoint(string input, string endpointName, string apiVersion, EndpointImplementationDelegate implementation) { AppInfo appInfo = null; HeContext heContext = null; MobileLoginInfoEndpoint loginEndpoint = null; Payload.ResponseVersionInfo responseVersionInfo = null; try { appInfo = AppInfo.GetAppInfo(); heContext = appInfo.OsContext; if (appInfo == null || !appInfo.IsApplicationEnabled) { return(GetScreenServiceResult(HttpStatusCode.ServiceUnavailable, GetExceptionPayload(new ApplicationBackendUnavailableException("Application Backend Unavailable")))); } ValidateRequestSecurity(); heContext.Session.EnableNewRuntimeSessionStorage(); loginEndpoint = ValidateRequestLogin(appInfo, heContext); Payload.RequestPayload requestPayload; try { requestPayload = JsonConvert.DeserializeObject <Payload.RequestPayload>(input, BehaviorsConfiguration.SerializerSettings); } catch (Exception ex) { throw GetBadRequestException("Failed to parse JSON request content.", ex); } if (requestPayload.VersionInfo == null) { throw GetBadRequestException("Missing Version Info in the request content."); } responseVersionInfo = new Payload.ResponseVersionInfo(requestPayload.VersionInfo, RunningInfo.ESpaceVersionToken, apiVersion); if (responseVersionInfo.HasApiVersionChanged) { return(GetScreenServiceResult(new Payload.EmptyDataPayload(), responseVersionInfo, loginEndpoint, heContext)); } else { SetCurrentStateFromHeaders(); object dataResponsePayload = implementation(heContext, requestPayload.ViewName, requestPayload.ScreenData, requestPayload.InputParameters); return(GetScreenServiceResult(dataResponsePayload, responseVersionInfo, loginEndpoint, heContext)); } } catch (Exception ex) { DatabaseAccess.FreeupResources(false); var licensingException = ex as LicensingException; if (licensingException != null) { // Already logged return(GetScreenServiceResult(HttpStatusCode.ServiceUnavailable, GetExceptionPayload(new ApplicationBackendUnavailableException("Application Backend Unavailable")))); } HttpStatusCode statusCode = HttpStatusCode.OK; var exposeRestException = ex as ExposeRestException; if (exposeRestException != null) { statusCode = exposeRestException.StatusCode; } // Currently for security reasons we log it here and don't let the handler in client side decide. This can generate many "non-errors" to be logged if exceptions are used for flow control string errorLogId = ErrorLog.LogApplicationError(ex, heContext, ""); var loggingContext = LoggingHelper.GetLoggingContext(); if (loggingContext != null) { loggingContext.ErrorLogId = errorLogId; } return(GetScreenServiceResult(statusCode, new Payload.EmptyDataPayload(), responseVersionInfo, GetExceptionPayload(ex), loginEndpoint, heContext)); } }