Advertisement
stuppid_bot

Untitled

Dec 14th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @apply https://vk.com/dev/errors
  2.  
  3. function camelize(str) { return str.replace(/\b(\w)/g, function (m) { return m.toUpperCase(); }); }
  4.  
  5. var rows = $$('.dev_param_row');
  6. var errors = [];
  7. var required_codes = [5, 6, 9, 10, 14, 15, 17, 113, 200, 201, 203, 300];
  8. var replace_names = {
  9.   UserAuthorizationFailed: 'AuthorizationFailed',
  10.   TooManyRequestsPerSecond: 'TooManyRequests',
  11.   InternalServerError: 'InternalError'
  12. }
  13.  
  14. for (i = 0; i<rows.length; ++i) {
  15.   var code = parseInt(rows[i].childNodes[0].innerHTML);
  16.   if (required_codes.indexOf(code) == -1)
  17.     continue;
  18.   var description = rows[i].childNodes[1].textContent;
  19.   description = description.split('\n');
  20.   description = description[0].trim();
  21.   var name = description
  22.   name = name.replace(/\b(is|was|this|occurr?ed|passed)\b/gi, '');
  23.   name = camelize(name).replace(/\W+/g, '');
  24.   var match = name.match(/^AccessTo(\w+)Denied$/);
  25.   if (match) {
  26.     name = match[1] + 'AccessDenied';
  27.   }
  28.   if (name in replace_names)
  29.     name = replace_names[name];
  30.   errors.push({code: code, name: name, description: description})
  31. }
  32.  
  33. console.log('\n' + JSON.stringify(errors, 0, 2));
  34.  
  35. var L = ["error = response['error']", "code = error['error_code']"];
  36. var L1 = ['# Специфические ошибки.'];
  37.  
  38. for (var i = 0; i<errors.length; ++i) {
  39.   var code = errors[i].code;
  40.   var name = errors[i].name;
  41.   var description = errors[i].description;
  42.   L.push(
  43.     (i ? 'el' : '') + 'if code == ' + code + ':',
  44.     '    error = ' + name + '(error)'
  45.   );
  46.   L1.push(
  47.     'class ' + name + '(ApiError):',
  48.     '    """' + description + '"""',
  49.     '    pass',
  50.     ''
  51.   );
  52. }
  53.  
  54. L.push('else:', '    error = ApiError(error)', 'raise error');
  55. L = L.concat(L1);
  56. console.log(L.join('\n'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement