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

Cambia respuesta acceso_rest ante errores #175

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions php/lib/toba_varios.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function utf8_e_seguro($s)
return $s;
}

return utf8_encode($s);
return mb_convert_encoding($s, 'UTF-8');
}

/**
Expand All @@ -27,7 +27,7 @@ function utf8_e_seguro($s)
function utf8_d_seguro($s)
{
if (mb_detect_encoding($s, "UTF-8", true) == "UTF-8") {
return utf8_decode($s);
return mb_convert_encoding($s, 'LATIN1', 'UTF-8');
}

return $s;
Expand Down
34 changes: 26 additions & 8 deletions php/nucleo/toba_nucleo.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,12 @@ function acceso_rest()
$this->solicitud->registrar();
$this->finalizar_contexto_rest();
} catch (Error $e) {
toba::logger()->crit($e, 'toba');
echo toba::escaper()->escapeJs($e->getMessage()) . "\n\n";
toba::logger()->crit($e, 'toba');
$this->echoErrorFormatoJson($e);
toba::logger()->guardar();
} catch (Exception $e) {
$codigo = $e->getCode();
if (is_int($codigo) && $codigo !== 0) {
header('Error Inesperado', true, 500);
}
toba::logger()->crit($e, 'toba');
echo toba::escaper()->escapeJs($e->getMessage()) . "\n\n";
toba::logger()->crit($e, 'toba');
$this->echoErrorFormatoJson($e);
toba::logger()->guardar();
}
if (! is_null($app)) {
Expand Down Expand Up @@ -615,6 +611,28 @@ protected function verifica_cambio_2FA($item)
return $item;
}

/**
* Envia un header con el status correspondiente
* y el mensaje de error en formato JSON
* @param type $e
*/
protected function echoErrorFormatoJson($e)
{
$status=500;
$codigo = $e->getCode();
if (is_int($codigo) && $codigo !== 0) {
$status = $codigo;
}

$msg = [
'error' => $status,
'mensaje' => 'Error interno',
'descripcion' => 'Error interno en el servidor'
];

header('Content-Type: application/json; charset=utf-8');
header('Error Inesperado', true, $status);
echo json_encode($msg);
}
}
?>