prodis / correios-cep

Find Brazilian addresses by zipcode, directly from Correios database. No HTML parsers.
Apache License 2.0
168 stars 33 forks source link

Return the reason of the error when a postal code is not found or invalid #28

Open prodis opened 3 years ago

prodis commented 3 years ago

Currently the response is an empty hash with no information about the error.

Some examples:

irb(main):014:0> Correios::CEP::AddressFinder.get('11111111')
I, [2021-04-04T17:34:31.227034 #31065]  INFO -- : > POST https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente
D, [2021-04-04T17:34:31.227074 #31065] DEBUG -- : Content-Type: text/xml; charset=utf-8
User-Agent: correios-cep/0.8.0
Connection: close
Host: apps.correios.com.br
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://cliente.bean.master.sigep.bsb.correios.com.br/"><soapenv:Header /><soapenv:Body><cli:consultaCEP><cep>11111111</cep></cli:consultaCEP></soapenv:Body></soapenv:Envelope>
I, [2021-04-04T17:34:33.045777 #31065]  INFO -- : < 500 Internal Server Error
D, [2021-04-04T17:34:33.045972 #31065] DEBUG -- : Date: Sun, 04 Apr 2021 15:34:31 GMT
Server: Apache/2.4.38 (Debian)
X-Opnet-Transaction-Trace: a2_6b81b0a1-0f4e-42e6-b1eb-69a1324ccccd
Content-Type: text/xml;charset=UTF-8
Content-Length: 359
Vary: Accept-Encoding,User-Agent
Set-Cookie: _op_aixPageId=a2_6b81b0a1-0f4e-42e6-b1eb-69a1324ccccd; Path=/
Set-Cookie: app-%3FINTERNO%3Fpool_proxy_app_sigep_443=POCCAIAK; Expires=Sun, 04-Apr-2021 15:54:32 GMT; Path=/
Connection: close
Set-Cookie: sto-id-%3FEXTERNO_2%3Fpool_Proxy_reverso_Apps_443=BIABKIMA; Expires=Sun, 04-Apr-2021 15:54:32 GMT; Path=/
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>CEP INVÁLIDO</faultstring><detail><ns2:SigepClienteException xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/">CEP INVÁLIDO</ns2:SigepClienteException></detail></soap:Fault></soap:Body></soap:Envelope>
=> {}

irb(main):015:0> Correios::CEP::AddressFinder.get('12345678')
I, [2021-04-04T17:35:11.670878 #31065]  INFO -- : > POST https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente
D, [2021-04-04T17:35:11.670915 #31065] DEBUG -- : Content-Type: text/xml; charset=utf-8
User-Agent: correios-cep/0.8.0
Connection: close
Host: apps.correios.com.br
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://cliente.bean.master.sigep.bsb.correios.com.br/"><soapenv:Header /><soapenv:Body><cli:consultaCEP><cep>12345678</cep></cli:consultaCEP></soapenv:Body></soapenv:Envelope>
I, [2021-04-04T17:35:13.230923 #31065]  INFO -- : < 500 Internal Server Error
D, [2021-04-04T17:35:13.231284 #31065] DEBUG -- : Date: Sun, 04 Apr 2021 15:35:11 GMT
Server: Apache/2.4.38 (Debian)
Content-Type: text/xml;charset=UTF-8
Content-Length: 369
Vary: Accept-Encoding,User-Agent
Set-Cookie: app-%3FINTERNO%3Fpool_proxy_app_sigep_443=POCCAIAK; Expires=Sun, 04-Apr-2021 15:55:12 GMT; Path=/
Connection: close
Set-Cookie: sto-id-%3FEXTERNO_2%3Fpool_Proxy_reverso_Apps_443=MOABKIMA; Expires=Sun, 04-Apr-2021 15:55:12 GMT; Path=/
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>CEP NAO ENCONTRADO</faultstring><detail><ns2:SigepClienteException xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/">CEP NAO ENCONTRADO</ns2:SigepClienteException></detail></soap:Fault></soap:Body></soap:Envelope>
=> {}

irb(main):016:0> Correios::CEP::AddressFinder.get('07600000')
I, [2021-04-04T17:37:01.617505 #31065]  INFO -- : > POST https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente
D, [2021-04-04T17:37:01.617545 #31065] DEBUG -- : Content-Type: text/xml; charset=utf-8
User-Agent: correios-cep/0.8.0
Connection: close
Host: apps.correios.com.br
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://cliente.bean.master.sigep.bsb.correios.com.br/"><soapenv:Header /><soapenv:Body><cli:consultaCEP><cep>07600000</cep></cli:consultaCEP></soapenv:Body></soapenv:Envelope>
I, [2021-04-04T17:37:03.211455 #31065]  INFO -- : < 200 OK
D, [2021-04-04T17:37:03.211887 #31065] DEBUG -- : Date: Sun, 04 Apr 2021 15:37:02 GMT
Server: Apache/2.4.38 (Debian)
X-Opnet-Transaction-Trace: a2_e4f932de-67a8-4e73-b42a-fa9729ea5cf0
Content-Type: text/xml;charset=UTF-8
Content-Length: 201
Vary: Accept-Encoding,User-Agent
Set-Cookie: _op_aixPageId=a2_e4f932de-67a8-4e73-b42a-fa9729ea5cf0; Path=/
Set-Cookie: app-%3FINTERNO%3Fpool_proxy_app_sigep_443=PNCCAIAK; Expires=Sun, 04-Apr-2021 15:57:02 GMT; Path=/
Set-Cookie: sto-id-%3FEXTERNO_2%3Fpool_Proxy_reverso_Apps_443=BIABKIMA; Expires=Sun, 04-Apr-2021 15:57:02 GMT; Path=/
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:consultaCEPResponse xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/"/></soap:Body></soap:Envelope>
=> {}
irb(main):017:0>
prodis commented 3 years ago

Similar error requests with error responses in the sibling Elixir library:

iex(1)> Correios.CEP.find_address("11111111")
{:error,
 %Correios.CEP.Error{
   message: "Unknown error",
   reason: "CEP INVÁLIDO",
   type: :unknown
 }}

iex(2)> Correios.CEP.find_address("12345678")
{:error,
 %Correios.CEP.Error{
   message: "Postal code not found",
   reason: "CEP NAO ENCONTRADO",
   type: :postal_code_not_found
 }}

iex(3)> Correios.CEP.find_address("07600000")
{:error,
 %Correios.CEP.Error{
   message: "Postal code not found",
   reason: "Empty response",
   type: :postal_code_not_found
 }}
prodis commented 3 years ago

Related to https://github.com/prodis/correios-cep/issues/27.