Permalink
Browse files

Catch errors in case of bad request

  • Loading branch information...
1 parent dc5aba4 commit 318ab1dd4c9c27e47246ee08e09b2850c340967a @Ayms committed Dec 14, 2011
Showing with 49 additions and 46 deletions.
  1. +49 −46 lib/bot.js
View
@@ -16,58 +16,61 @@ var getElements = function(query,response){
var resp={
end : function(res) {
-
- if (res.length) {
- var url=res[0].url;
- var p='url='+url+'&name='+params.name;
- url=url.split('/')[2];
- var t=params.search.toLowerCase().split(' ');
- var search=[];
- var l=t.length;
- for (var i=0;i<l;i++) {
- var re=new RegExp(t[i],"g");
- if (!re.test(url)) {
- search.push(t[i]);
- }
- };
- search=search.join(' ');
- };
- //Example : babyliss g910e cdiscount
- //becomes : babyliss g910e
-
- if (params.gadget) {
- p +='&fetch='+$E('{"img":"", "input":""}')+'&proc='+$E('{"img":"","input":""}')+'&gadget=true&price=true&search='+search+'&regexp='+$E(params.regexp);
- } else {
- p +='&fetch='+$E('{"img":"", "input":""}')+'&proc='+$E('{"img":"","input":""}')+'&price=true&search='+search+'&regexp='+$E(params.regexp);
- };
+ try {
+ if (res.length) {
+ var url=res[0].url;
+ var p='url='+url+'&name='+params.name;
+ url=url.split('/')[2];
+ var t=params.search.toLowerCase().split(' ');
+ var search=[];
+ var l=t.length;
+ for (var i=0;i<l;i++) {
+ var re=new RegExp(t[i],"g");
+ if (!re.test(url)) {
+ search.push(t[i]);
+ }
+ };
+ search=search.join(' ');
+ };
+ //Example : babyliss g910e cdiscount
+ //becomes : babyliss g910e
+
+ if (params.gadget) {
+ p +='&fetch='+$E('{"img":"", "input":""}')+'&proc='+$E('{"img":"","input":""}')+'&gadget=true&price=true&search='+search+'&regexp='+$E(params.regexp);
+ } else {
+ p +='&fetch='+$E('{"img":"", "input":""}')+'&proc='+$E('{"img":"","input":""}')+'&price=true&search='+search+'&regexp='+$E(params.regexp);
+ };
- //console.log(p);
+ //console.log(p);
- var resp2={
-
- end : function(res) {
-
- var head = {'Content-Type': 'text/javascript' };
-
- if (response.writeHead) {
-
- response.writeHead(200, head);
+ var resp2={
- response.end(params.name+'='+JSON.stringify(res)+';');
+ end : function(res) {
- /*var l=res.length;
- for (var i=0;i<l;i++) {
- console.log(res[i][3]+' '+res[i][6]);
- }*/
-
- } else {
- response.end(res);
- }
- }
- };
+ var head = {'Content-Type': 'text/javascript' };
+
+ if (response.writeHead) {
+
+ response.writeHead(200, head);
+
+ response.end(params.name+'='+JSON.stringify(res)+';');
+
+ /*var l=res.length;
+ for (var i=0;i<l;i++) {
+ console.log(res[i][3]+' '+res[i][6]);
+ }*/
+
+ } else {
+ response.end(res);
+ }
+ }
+ };
- genGadgets(p,resp2);
+ genGadgets(p,resp2);
+ } catch(ee) {
+ response.end('Bad formatted request');
+ }
}
};

0 comments on commit 318ab1d

Please sign in to comment.