Tweaked error handling

This commit is contained in:
2022-02-25 05:21:58 +00:00
parent a9dc7df2b1
commit aed080f531
3 changed files with 56 additions and 17 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "sineql", "name": "sineql",
"version": "0.5.2", "version": "0.5.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "sineql", "name": "sineql",
"version": "0.5.2", "version": "0.5.3",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"jest": "^27.5.1" "jest": "^27.5.1"

View File

@@ -1,6 +1,6 @@
{ {
"name": "sineql", "name": "sineql",
"version": "0.5.2", "version": "0.5.3",
"description": "A simple to use graphQL clone", "description": "A simple to use graphQL clone",
"main": "source/index.js", "main": "source/index.js",
"scripts": { "scripts": {

View File

@@ -23,8 +23,8 @@ const sineQL = (schema, { queryHandlers, createHandlers, updateHandlers, deleteH
//parse the query //parse the query
const tokens = parseInput(reqBody, true, options); const tokens = parseInput(reqBody, true, options);
//check for leading keywords (create, update, delete)
switch(tokens[0]) { switch(tokens[0]) {
//check for leading keywords
case 'create': { case 'create': {
if (!createHandlers) { if (!createHandlers) {
return [405, 'Create handlers not implemented']; return [405, 'Create handlers not implemented'];
@@ -36,10 +36,21 @@ const sineQL = (schema, { queryHandlers, createHandlers, updateHandlers, deleteH
const createTree = parseCreateTree(tokens, typeGraph, options); const createTree = parseCreateTree(tokens, typeGraph, options);
try {
const result = await createHandlers[tokens[1]](createTree, typeGraph); const result = await createHandlers[tokens[1]](createTree, typeGraph);
if (options.debug) {
console.log('Create tree results:');
console.dir(result, { depth: null });
}
return [200, result]; return [200, result];
} }
catch(e) {
console.error('Create error:', e);
return [400, e];
}
}
case 'update': { case 'update': {
if (!updateHandlers) { if (!updateHandlers) {
@@ -52,10 +63,21 @@ const sineQL = (schema, { queryHandlers, createHandlers, updateHandlers, deleteH
const updateTree = parseUpdateTree(tokens, typeGraph, options); const updateTree = parseUpdateTree(tokens, typeGraph, options);
try {
const result = await updateHandlers[tokens[1]](updateTree, typeGraph); const result = await updateHandlers[tokens[1]](updateTree, typeGraph);
if (options.debug) {
console.log('Update tree results:');
console.dir(result, { depth: null });
}
return [200, result]; return [200, result];
} }
catch(e) {
console.error('Update error:', e);
return [400, e];
}
}
case 'delete': { case 'delete': {
if (!deleteHandlers) { if (!deleteHandlers) {
@@ -68,10 +90,21 @@ const sineQL = (schema, { queryHandlers, createHandlers, updateHandlers, deleteH
const deleteTree = parseDeleteTree(tokens, typeGraph, options); const deleteTree = parseDeleteTree(tokens, typeGraph, options);
try {
const result = await deleteHandlers[tokens[1]](deleteTree, typeGraph); const result = await deleteHandlers[tokens[1]](deleteTree, typeGraph);
if (options.debug) {
console.log('Delete tree results:');
console.dir(result, { depth: null });
}
return [200, result]; return [200, result];
} }
catch(e) {
console.error('Delete error:', e);
return [400, e];
}
}
//no leading keyword - regular query //no leading keyword - regular query
default: { default: {
@@ -85,6 +118,7 @@ const sineQL = (schema, { queryHandlers, createHandlers, updateHandlers, deleteH
const queryTree = parseQueryTree(tokens, typeGraph, options); const queryTree = parseQueryTree(tokens, typeGraph, options);
try {
const result = await queryHandlers[queryTree.typeName](queryTree, typeGraph); const result = await queryHandlers[queryTree.typeName](queryTree, typeGraph);
if (options.debug) { if (options.debug) {
@@ -94,10 +128,15 @@ const sineQL = (schema, { queryHandlers, createHandlers, updateHandlers, deleteH
return [200, result]; return [200, result];
} }
catch(e) {
console.error('Query error:', e);
return [400, e];
}
}
} }
} }
catch(e) { catch(e) {
console.error('Error:', e); console.error('Input error:', e);
return [400, e]; return [400, e];
} }
}; };