From 2f6146c0f70ebf1a204734741abc6b8b68c80d69 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Fri, 23 Jan 2015 13:26:34 +0100 Subject: [PATCH] * Web.csproj: * LocalizedText.resx: * SimpleFormatter.cs: * TemplateException.cs: * ErrorHtmlFormatter.cs: * LocalizedText.Designer.cs: * WorkFlowManager.cs: * Makefile: refactoring * FrontOfficeApiController.cs: nice html error response in case of error at rendering a tex or pdf document * LocalizedText.fr.resx: Gives as title at rendering a template processing error --- Makefile | 11 ++- web/Controllers/FrontOfficeApiController.cs | 28 ++++++-- web/Controllers/TemplateException.cs | 40 +++++++++++ web/Formatters/ErrorHtmlFormatter.cs | 74 +++++++++++++++++++++ web/Formatters/SimpleFormatter.cs | 3 + web/Web.csproj | 4 +- yavscModel/LocalizedText.Designer.cs | 6 ++ yavscModel/LocalizedText.fr.resx | 2 + yavscModel/LocalizedText.resx | 2 + yavscModel/WorkFlow/WorkFlowManager.cs | 6 +- 10 files changed, 163 insertions(+), 13 deletions(-) create mode 100644 web/Controllers/TemplateException.cs create mode 100644 web/Formatters/ErrorHtmlFormatter.cs diff --git a/Makefile b/Makefile index 815600cf..5f5fa4a2 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ VERSION=1.1 CONFIG=Debug DESTDIR=build/web/$(CONFIG) COPYUNCHANGED="false" -PREPRODHOST=yavsc.localdomain - +PREPRODHOSTDIR=localhost:/srv/www/yavsc +PRODHOSTDIR=localhost:/srv/www/lua all: deploy ddir: @@ -25,13 +25,10 @@ clean: rm -rf $(DESTDIR) rsync-preprod: deploy - rsync -ravu build/web/$(CONFIG)/ root@$(PREPRODHOST):/srv/www/yavsc + rsync -ravu build/web/$(CONFIG)/ root@$(PREPRODHOSTDIR) rsync-prod: deploy - rsync -ravu build/web/$(CONFIG)/ root@$(PREPRODHOST):/srv/www/lua - -rsync-local: - rsync -ravu build/web/$(CONFIG)/ root@localhost:/srv/www/yavsc + rsync -ravu build/web/$(CONFIG)/ root@$(PRODHOSTDIR) sourcepkg: git archive --format=tar --prefix=yavsc-$(CONFIG)/ $(CONFIG) | bzip2 > yavsc-$(CONFIG).tar.bz2 diff --git a/web/Controllers/FrontOfficeApiController.cs b/web/Controllers/FrontOfficeApiController.cs index 732ec7a0..c3695a3c 100644 --- a/web/Controllers/FrontOfficeApiController.cs +++ b/web/Controllers/FrontOfficeApiController.cs @@ -22,6 +22,7 @@ using Yavsc.Formatters; using System.Text; using System.Web.Profile; using System.Collections.Specialized; +using Yavsc.Model; namespace Yavsc.ApiControllers { @@ -102,9 +103,28 @@ namespace Yavsc.ApiControllers [AcceptVerbs("GET")] public HttpResponseMessage GetEstimTex(long estimid) { - string texest = getEstimTex (estimid); + string texest = null; + try { + texest = getEstimTex (estimid); + } + catch (TemplateException ex) { + return new HttpResponseMessage (HttpStatusCode.OK){ Content = + new ObjectContent (typeof(string), + ex.Message, new ErrorHtmlFormatter(HttpStatusCode.NotAcceptable, + LocalizedText.DocTemplateException + ))}; + } + catch (Exception ex) { + return new HttpResponseMessage (HttpStatusCode.OK){ Content = + new ObjectContent (typeof(string), + ex.Message, new SimpleFormatter("text/text")) }; + } if (texest == null) - throw new HttpRequestValidationException ("Not an estimation id:"+estimid); + return new HttpResponseMessage (HttpStatusCode.OK) { Content = + new ObjectContent (typeof(string), + "Not an estimation id:" + estimid, new SimpleFormatter ("text/text")) + }; + return new HttpResponseMessage () { Content = new ObjectContent (typeof(string), texest, @@ -121,11 +141,11 @@ namespace Yavsc.ApiControllers Profile prpro = new Profile(ProfileBase.Create(e.Responsible)); if (!prpro.IsBankable) - throw new Exception ("NotBankable:"+e.Responsible); + throw new TemplateException ("NotBankable:"+e.Responsible); Profile prcli = new Profile(ProfileBase.Create(e.Client)); if (!prcli.IsBillable) - throw new Exception ("NotBillable:"+e.Client); + throw new TemplateException ("NotBillable:"+e.Client); tmpe.Session.Add ("from", prpro); tmpe.Session.Add ("to", prcli); tmpe.Init (); diff --git a/web/Controllers/TemplateException.cs b/web/Controllers/TemplateException.cs new file mode 100644 index 00000000..87806ec5 --- /dev/null +++ b/web/Controllers/TemplateException.cs @@ -0,0 +1,40 @@ +using System; +using Yavsc; +using SalesCatalog; +using SalesCatalog.Model; +using System.Web.Routing; +using System.Threading.Tasks; +using System.Diagnostics; +using System.Web.Http; +using System.Net.Http; +using System.Web; +using System.Linq; +using System.IO; +using System.Net; +using WorkFlowProvider; +using System.Web.Security; +using Yavsc.Model.WorkFlow; +using System.Reflection; +using System.Collections.Generic; +using Yavsc.Model.RolesAndMembers; +using Yavsc.Controllers; +using Yavsc.Formatters; +using System.Text; +using System.Web.Profile; +using System.Collections.Specialized; + +namespace Yavsc.ApiControllers +{ + + class TemplateException : Exception + { + public TemplateException(string message):base(message) + { + } + public TemplateException(string message,Exception innerException):base(message,innerException) + { + } + } + +} + diff --git a/web/Formatters/ErrorHtmlFormatter.cs b/web/Formatters/ErrorHtmlFormatter.cs new file mode 100644 index 00000000..2ea24977 --- /dev/null +++ b/web/Formatters/ErrorHtmlFormatter.cs @@ -0,0 +1,74 @@ +// +// ErrorHtmlFormatter.cs +// +// Author: +// paul <${AuthorEmail}> +// +// Copyright (c) 2015 paul +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +using System; +using System.Net.Http.Formatting; +using System.Net.Http.Headers; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Web.Mvc; +using System.Net; + +namespace Yavsc.Formatters +{ + + public class ErrorHtmlFormatter:SimpleFormatter { + public string Title { get ; set; } + public HttpStatusCode ErrorCode { get ; set; } + string doctype=""; + public ErrorHtmlFormatter + (HttpStatusCode errorCode, string title):base("text/html") + { + ErrorCode = errorCode; + Title = title; + + } + + public override void WriteToStream (Type type, object value, Stream stream, HttpContentHeaders contentHeaders) + { + // TODO create a type containing T4 parameters, and generate from them + using (var writer = new StreamWriter(stream)) + { + string message = value as string; + TagBuilder doc = new TagBuilder ("html"); + TagBuilder body = new TagBuilder ("body"); + TagBuilder h1 = new TagBuilder ("h1"); + TagBuilder p = new TagBuilder ("p"); + TagBuilder head = new TagBuilder ("head"); + head.InnerHtml = "" + + "" + + ""; + p.InnerHtml = MvcHtmlString.Create (message).ToHtmlString(); + h1.InnerHtml = MvcHtmlString.Create (Title).ToHtmlString(); + body.InnerHtml = h1.ToString()+p.ToString (); + doc.InnerHtml = head.ToString()+"\n"+body.ToString (); + writer.WriteLine (doctype); + writer.Write (doc.ToString()); + } + + } + + } +} diff --git a/web/Formatters/SimpleFormatter.cs b/web/Formatters/SimpleFormatter.cs index 1db1a974..3506de19 100644 --- a/web/Formatters/SimpleFormatter.cs +++ b/web/Formatters/SimpleFormatter.cs @@ -24,6 +24,8 @@ using System.Net.Http.Headers; using System.Collections.Generic; using System.IO; using System.Net.Http; +using System.Web.Mvc; +using System.Net; namespace Yavsc.Formatters { @@ -63,5 +65,6 @@ namespace Yavsc.Formatters } } + } diff --git a/web/Web.csproj b/web/Web.csproj index ebd7d4fc..ea066ec8 100644 --- a/web/Web.csproj +++ b/web/Web.csproj @@ -172,6 +172,8 @@ + + @@ -639,7 +641,7 @@ - + diff --git a/yavscModel/LocalizedText.Designer.cs b/yavscModel/LocalizedText.Designer.cs index b8218700..5a983db4 100644 --- a/yavscModel/LocalizedText.Designer.cs +++ b/yavscModel/LocalizedText.Designer.cs @@ -154,6 +154,12 @@ namespace Yavsc.Model { } } + public static string DocTemplateException { + get { + return ResourceManager.GetString("DocTemplateException", resourceCulture); + } + } + public static string Description { get { return ResourceManager.GetString("Description", resourceCulture); diff --git a/yavscModel/LocalizedText.fr.resx b/yavscModel/LocalizedText.fr.resx index c75e09da..e3bc159c 100644 --- a/yavscModel/LocalizedText.fr.resx +++ b/yavscModel/LocalizedText.fr.resx @@ -39,4 +39,6 @@ Date maximale du rendez-vous Demande de rendez-vous Se souvenir du mot de passe + Une erreur est survenue à la génération de votre document + diff --git a/yavscModel/LocalizedText.resx b/yavscModel/LocalizedText.resx index 72b8d633..3b8a7d98 100644 --- a/yavscModel/LocalizedText.resx +++ b/yavscModel/LocalizedText.resx @@ -39,4 +39,6 @@ Maximal date for the rendez-vous Rendez-vous query Remember me + Exception occured when rendering your document + diff --git a/yavscModel/WorkFlow/WorkFlowManager.cs b/yavscModel/WorkFlow/WorkFlowManager.cs index b5f2a550..c6173fb4 100644 --- a/yavscModel/WorkFlow/WorkFlowManager.cs +++ b/yavscModel/WorkFlow/WorkFlowManager.cs @@ -20,7 +20,11 @@ namespace Yavsc.Model.WorkFlow { ContentProvider.UpdateEstimate (estim); } - + /// + /// Gets the estimate. + /// + /// The estimate. + /// Estid. public Estimate GetEstimate (long estid) { return ContentProvider.GetEstimate (estid);