Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with InternalRender #49

Closed
ezequieljuliano opened this issue Sep 5, 2016 · 3 comments
Closed

Problem with InternalRender #49

ezequieljuliano opened this issue Sep 5, 2016 · 3 comments

Comments

@ezequieljuliano
Copy link
Collaborator

In unit MVCFramework.pas exists the following method:

procedure InternalRender(aJSONValue: TJSONValue;
  ContentType, ContentEncoding: string; Context: TWebContext;
  aInstanceOwner: Boolean);
var
  OutEncoding: TEncoding;
  JString: string;
begin
{$IF CompilerVersion <= 27}
  JString := aJSONValue.ToString; // requires the patch
{$ELSE}
  JString := aJSONValue.ToJSON; // since XE7 it works using ToJSON
{$ENDIF}
  // first set the ContentType; because of this bug:
  // http://qc.embarcadero.com/wc/qcmain.aspx?d=67350
  Context.Response.RawWebResponse.ContentType := ContentType + '; charset=' +
    ContentEncoding;

  OutEncoding := TEncoding.GetEncoding(ContentEncoding);
  try
    Context.Response.RawWebResponse.Content :=
      OutEncoding.GetString(TEncoding.Convert(TEncoding.Default, OutEncoding,
      TEncoding.Default.GetBytes(JString)));
  finally
    OutEncoding.Free;
  end;

  if aInstanceOwner then
    FreeAndNil(aJSONValue)
end;

When an exception is raised on my server (the error message contains special characters) the line
JString := aJSONValue.ToJSON; (My case I'm with X7) It is not right encoding the message.
If I use JString := aJSONValue.ToString; The conversion is done correctly.

For example. The following error message is generated on the server:
'{"status":"error","classname":"EACBrNFeException","message":"Falha na validação dos dados da nota: 0'#$D#$A'TAG: ID:B04/natOp(Descrição da Natureza da Operação) - Nenhum valor informado.'#$D#$A'TAG: ID:B12/cMunFG(Código do Município FG) - Conteúdo inválido.'#$D#$A'TAG: ID:B04/natOp(Descrição da Natureza da Operação) - Nenhum valor informado.'#$D#$A'TAG: ID:B12/cMunFG(Código do Município FG) - Conteúdo inválido.'#$D#$A#$D#$A''''' violates pattern constraint of ''[!-ÿ]{1}[ -ÿ]{0,}[!-ÿ]{1}|[!-ÿ]{1}''.'#$D#$A'The element ''{http://www.portalfiscal.inf.br/nfe}natOp'' with value '''' failed to parse.'#$D#$A'","http_error":417}'

When the system crashes in the JString := aJSONValue.ToJSON; message is like:
'{"status":"error","classname":"EACBrNFeException","message":"Falha na valida\u00E7\u00E3o dos dados da nota: 0\r\nTAG: ID:B04/natOp(Descri\u00E7\u00E3o da Natureza da Opera\u00E7\u00E3o) - Nenhum valor informado.\r\nTAG: ID:B12/cMunFG(C\u00F3digo do Munic\u00EDpio FG) - Conte\u00FAdo inv\u00E1lido.\r\nTAG: ID:B04/natOp(Descri\u00E7\u00E3o da Natureza da Opera\u00E7\u00E3o) - Nenhum valor informado.\r\nTAG: ID:B12/cMunFG(C\u00F3digo do Munic\u00EDpio FG) - Conte\u00FAdo inv\u00E1lido.\r\n\r\n'''' violates pattern constraint of ''[!-\u00FF]{1}[ -\u00FF]{0,}[!-\u00FF]{1}|[!-\u00FF]{1}''.\r\nThe element ''{http://www.portalfiscal.inf.br/nfe}natOp'' with value '''' failed to parse.\r\n","http_error":417}'

If I leave the only JString := aJSONValue.ToString; the client receives the message correctly.

My question is whether this compilation directive is really necessary. I would not to leave only JString := aJSONValue.ToString;

@danieleteti
Copy link
Owner

danieleteti commented Sep 8, 2016

It seems to be the correct bahaviour. TJSONValue.ToString is not intended to produce correct JSON but only for debug purpouse .
The correct way to get correct encoded JSON is TJSONValue.ToJSON.
Can you give me a mve of your issue?

@ezequieljuliano
Copy link
Collaborator Author

DMVCError.zip

I have attached an example. I believe the problem lies in converting UTF-8 in RESTClient.

To simulate. Run the server. Open Client and click Execute.

Special characters are not converted.

@danieleteti
Copy link
Owner

Hi Ezequiel,

the "garbage" you see is the JSON encoded string. You have to parse that
string as JSON and then get the error message.
Since some months I added a small authomatism about it. Check the attached
project.

P.S. Did you have some tested code to implement output compression in
DMVCFramework? I've a middleware to do that, but still not tested enough.
Let me know.

See you

DT

2016-09-12 19:49 GMT+02:00 Ezequiel Juliano Müller <notifications@github.com

:

DMVCError.zip
https://github.com/danieleteti/delphimvcframework/files/467973/DMVCError.zip

I have attached an example. I believe the problem lies in converting UTF-8
in RESTClient.

To simulate. Run the server. Open Client and click Execute.

Special characters are not converted.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#49 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABXMpIEqhPWuzr1rcZDBscBS9clK0ndFks5qpZCugaJpZM4J1QMA
.

Daniele Teti
CEO
bit Time Professionals
web: www.bittimeprofessionals.it http://www.bittime.it
blog: www.danieleteti.it
books: www.danieleteti.it/delphi-cookbook
mobile: +39 3496626822
office: +39 06 83393029

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants