upd: api.core.Request (now checks for default param if not FILE nor NULL + added WrongDefaultParam (Err constant) + replaced 'name' by 'rename' for error handling)

This commit is contained in:
xdrm-brackets 2017-12-11 18:42:56 +01:00
parent 15e28c10ec
commit be583ea9ea
4 changed files with 47 additions and 25 deletions

View File

@ -327,13 +327,24 @@
if( isset($config['rename']) && is_string($config['rename']) && preg_match('@^\w+$@', $config['rename']) )
$rename = $config['rename'];
/* (2) Gestion du paramètre DEFAULT */
/* (2) On récupère si le paramètre est optionnel ou pas */
$optional = isset($config['optional']) && $config['optional'] === true;
/* (3) Gestion du paramètre DEFAULT */
$default = null;
if( isset($config['default']) && is_string($config['default']) )
/* (3.1) Check if default NOT (NULL || FILE) -> matches TYPE */
if( isset($config['default']) ){
/* (3.1.1) Set default value from config */
$default = $config['default'];
/* (3) On récupère si le paramètre est optionnel ou pas */
$optional = isset($config['optional']) && $config['optional'] === true;
/* (3.1.2) If FILE and not null -> Check type */
if( $config['type'] != 'FILE' || $default != null )
if( !Checker::run($config['type'], $default) )
return $this->error->set(Err::WrongDefaultParam, $rename, $config['type']);
}
/* (4) Si de type 'FILE' + fichier existe => on enregistre la ref. */
if( $config['type'] == 'FILE' && isset($_FILES[$name]) )
@ -341,7 +352,7 @@
/* (4) Si param obligatoire et manquant -> erreur */
if( !isset($this->raw_params[$name]) && !$optional )
return $this->error->set(Err::MissingParam, $name);
return $this->error->set(Err::MissingParam, $rename);
/* (2.3) Gestion des valeurs
@ -357,7 +368,7 @@
// Si la verification est fausse, on retourne faux
if( !Checker::run($config['type'], $this->raw_params[$name]) )
return $this->error->set(Err::WrongParam, $name, $config['type']);
return $this->error->set(Err::WrongParam, $rename, $config['type']);
// Sinon, on ajoute aux params qu'on enverra à l'appel
else

View File

@ -61,10 +61,12 @@
const MissingParam = 16;
/* (11) Paramètre incorrect */
const WrongParam = 17;
/* (12) Erreur dans le traitement */
const ModuleError = 18;
/* (13) URI Invalide */
const InvalidURI = 19;
/* (12) Valeur par défaut incorrecte */
const WrongDefaultParam = 18;
/* (13) Erreur dans le traitement */
const ModuleError = 19;
/* (14) URI Invalide */
const InvalidURI = 20;
/* [5] Database
@ -72,50 +74,50 @@
/* (1) Base de données
---------------------------------------------------------*/
/* (1) Erreur lors de la creation d'un objet PDO (connection) */
const PDOConnection = 20;
const PDOConnection = 21;
/* (2) Repositories
---------------------------------------------------------*/
/* (1) Verification de la coherence du chemin (existe dans la conf) */
const WrongPathRepo = 21;
const WrongPathRepo = 22;
/* (2) Module non specifie dans la conf */
const UnknownRepo = 22;
const UnknownRepo = 23;
/* (3) Erreur dans le traitement */
const RepoError = 23;
const RepoError = 24;
/* (3) ORM
---------------------------------------------------------*/
/* (1) Table n'existe pas */
const UnknownTable = 24;
const UnknownTable = 25;
/* (2) Pas permissions de lire le schéma */
const NotAllowedSchema = 25;
const NotAllowedSchema = 26;
/* [6] Erreurs diverses
=========================================================*/
/* (1) Aucune donnée trouvée */
const NoMatchFound = 26;
const NoMatchFound = 27;
/* (2) Mauvais chemin de template */
const UnknownTemplate = 27;
const UnknownTemplate = 28;
/* (3) géolocalisation échouée */
const UnknownAddress = 28;
const UnknownAddress = 29;
/* (4) Erreur inconnue */
const UnknownError = 29;
const UnknownError = 30;
/* (5) Entrée existante */
const AlreadyExists = 30;
const AlreadyExists = 31;
/* (6) Corps manquant */
const MissingBody = 31;
const MissingBody = 32;
/* (7) Header manquant */
const MissingHeaders = 32;
const MissingHeaders = 33;
}
?>

View File

@ -77,6 +77,7 @@
case Err::ConfigError: return $this->ConfigError(); break;
case Err::MissingParam: return $this->MissingParam(); break;
case Err::WrongParam: return $this->WrongParam(); break;
case Err::WrongDefaultParam: return $this->WrongDefaultParam(); break;
case Err::ModuleError: return $this->ModuleError(); break;
case Err::InvalidURI: return $this->InvalidURI(); break;
case Err::PDOConnection: return $this->PDOConnection(); break;
@ -161,6 +162,14 @@
return 'wrong param \''.$this->arguments[0].'\'';
else
return 'wrong param';
}private function WrongDefaultParam(){
if( count($this->arguments) > 0 )
if( count($this->arguments) > 1 )
return 'wrong default param \''.$this->arguments[0].'\' expected to be of type \''.$this->arguments[1].'\'';
else
return 'wrong default param \''.$this->arguments[0].'\'';
else
return 'wrong default param';
}private function ModuleError(){
if( count($this->arguments) > 0 )
return 'module error: \''.$this->arguments[0].'\'';

View File

@ -3,7 +3,7 @@
"description": "Returns the API documentation",
"permissions": [],
"parameters": {
"URL0": { "description": "Name of the project to release", "type": "alphanumeric", "rename": "var1", "optional": true, "default": "default_value" }
"URL0": { "description": "Method name", "type": "varchar(1,30)", "rename": "method_name", "optional": true, "default": null, "default": null }
}
},