Response.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { RequestError } from 'got'
  2. /**
  3. * @see https://wiki.vg/Authentication#Errors
  4. */
  5. export enum MojangResponseCode {
  6. SUCCESS,
  7. ERROR,
  8. ERROR_METHOD_NOT_ALLOWED, // INTERNAL
  9. ERROR_NOT_FOUND, // INTERNAL
  10. ERROR_USER_MIGRATED,
  11. ERROR_INVALID_CREDENTIALS,
  12. ERROR_RATELIMIT,
  13. ERROR_INVALID_TOKEN,
  14. ERROR_ACCESS_TOKEN_HAS_PROFILE, // ??
  15. ERROR_CREDENTIALS_ARE_NULL, // INTERNAL
  16. ERROR_INVALID_SALT_VERSION, // ??
  17. ERROR_UNSUPPORTED_MEDIA_TYPE // INTERNAL
  18. }
  19. export interface MojangResponse<T> {
  20. data: T
  21. responseCode: MojangResponseCode
  22. error?: RequestError
  23. isInternalError?: boolean
  24. }
  25. export interface MojangErrorBody {
  26. error: string
  27. errorMessage: string
  28. cause?: string
  29. }
  30. export function deciperResponseCode(body: MojangErrorBody): MojangResponseCode {
  31. if(body.error === 'Method Not Allowed') {
  32. return MojangResponseCode.ERROR_METHOD_NOT_ALLOWED
  33. } else if(body.error === 'Not Found') {
  34. return MojangResponseCode.ERROR_NOT_FOUND
  35. } else if(body.error === 'Unsupported Media Type') {
  36. return MojangResponseCode.ERROR_UNSUPPORTED_MEDIA_TYPE
  37. } else if(body.error === 'ForbiddenOperationException') {
  38. if(body.cause && body.cause === 'UserMigratedException') {
  39. return MojangResponseCode.ERROR_USER_MIGRATED
  40. }
  41. if(body.errorMessage === 'Invalid credentials. Invalid username or password.') {
  42. return MojangResponseCode.ERROR_INVALID_CREDENTIALS
  43. } else if(body.errorMessage === 'Invalid credentials.') {
  44. return MojangResponseCode.ERROR_RATELIMIT
  45. } else if(body.errorMessage === 'Invalid token.') {
  46. return MojangResponseCode.ERROR_INVALID_TOKEN
  47. }
  48. } else if(body.error === 'IllegalArgumentException') {
  49. if(body.errorMessage === 'Access token already has a profile assigned.') {
  50. return MojangResponseCode.ERROR_ACCESS_TOKEN_HAS_PROFILE
  51. } else if(body.errorMessage === 'credentials is null') {
  52. return MojangResponseCode.ERROR_CREDENTIALS_ARE_NULL
  53. } else if(body.errorMessage === 'Invalid salt version') {
  54. return MojangResponseCode.ERROR_INVALID_SALT_VERSION
  55. }
  56. }
  57. return MojangResponseCode.ERROR
  58. }
  59. // These indicate problems with the code and not the data.
  60. export function isInternalError(responseCode: MojangResponseCode): boolean {
  61. switch(responseCode) {
  62. case MojangResponseCode.ERROR_METHOD_NOT_ALLOWED: // We've sent the wrong method to an endpoint. (ex. GET to POST)
  63. case MojangResponseCode.ERROR_NOT_FOUND: // Indicates endpoint has changed. (404)
  64. case MojangResponseCode.ERROR_ACCESS_TOKEN_HAS_PROFILE: // Selecting profiles isn't implemented yet. (Shouldnt happen)
  65. case MojangResponseCode.ERROR_CREDENTIALS_ARE_NULL: // Username/password was not submitted. (UI should forbid this)
  66. case MojangResponseCode.ERROR_INVALID_SALT_VERSION: // ??? (Shouldnt happen)
  67. case MojangResponseCode.ERROR_UNSUPPORTED_MEDIA_TYPE: // Data was not submitted as application/json
  68. return true
  69. default:
  70. return false
  71. }
  72. }