common.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import * as express from 'express'
  2. import { CLIError } from '@oclif/errors'
  3. import { ExtrinsicFailedError } from '../../runtime/api'
  4. import { BagIdValidationError } from '../../helpers/bagTypes'
  5. /**
  6. * Dedicated error for the web api requests.
  7. */
  8. export class WebApiError extends CLIError {
  9. httpStatusCode: number
  10. constructor(err: string, httpStatusCode: number) {
  11. super(err)
  12. this.httpStatusCode = httpStatusCode
  13. }
  14. }
  15. /**
  16. * Dedicated server error for the web api requests.
  17. */
  18. export class ServerError extends WebApiError {
  19. constructor(err: string) {
  20. super(err, 500)
  21. }
  22. }
  23. /**
  24. * Returns a directory for file uploading from the response.
  25. *
  26. * @remarks
  27. * This is a helper function. It parses the response object for a variable and
  28. * throws an error on failure.
  29. */
  30. export function getUploadsDir(res: express.Response): string {
  31. if (res.locals.uploadsDir) {
  32. return res.locals.uploadsDir
  33. }
  34. throw new ServerError('No upload directory path loaded.')
  35. }
  36. /**
  37. * Returns a directory for temporary file uploading from the response.
  38. *
  39. * @remarks
  40. * This is a helper function. It parses the response object for a variable and
  41. * throws an error on failure.
  42. */
  43. export function getTempFileUploadingDir(res: express.Response): string {
  44. if (res.locals.tempFileUploadingDir) {
  45. return res.locals.tempFileUploadingDir
  46. }
  47. throw new ServerError('No temporary uploading directory path loaded.')
  48. }
  49. /**
  50. * Returns worker ID from the response.
  51. *
  52. * @remarks
  53. * This is a helper function. It parses the response object for a variable and
  54. * throws an error on failure.
  55. */
  56. export function getWorkerId(res: express.Response): number {
  57. if (res.locals.workerId || res.locals.workerId === 0) {
  58. return res.locals.workerId
  59. }
  60. throw new ServerError('No Joystream worker ID loaded.')
  61. }
  62. /**
  63. * Returns the QueryNode URL from the starting parameters.
  64. *
  65. * @remarks
  66. * This is a helper function. It parses the response object for a variable and
  67. * throws an error on failure.
  68. */
  69. export function getQueryNodeUrl(res: express.Response): string {
  70. if (res.locals.queryNodeUrl) {
  71. return res.locals.queryNodeUrl
  72. }
  73. throw new ServerError('No Query Node URL loaded.')
  74. }
  75. /**
  76. * Returns a command config.
  77. *
  78. * @remarks
  79. * This is a helper function. It parses the response object for a variable and
  80. * throws an error on failure.
  81. */
  82. export function getCommandConfig(res: express.Response): {
  83. version: string
  84. userAgent: string
  85. } {
  86. if (res.locals.config) {
  87. return res.locals.config
  88. }
  89. throw new ServerError('Cannot load command config.')
  90. }
  91. /**
  92. * Handles errors and sends a response.
  93. *
  94. * @param res - Response instance
  95. * @param err - error
  96. * @param errorType - defines request type
  97. * @returns void promise.
  98. */
  99. export function sendResponseWithError(res: express.Response, err: Error, errorType: string): void {
  100. const message = isNofileError(err) ? `File not found.` : err.toString()
  101. res.status(getHttpStatusCodeByError(err)).json({
  102. type: errorType,
  103. message,
  104. })
  105. }
  106. /**
  107. * Checks the error for 'no-file' error (ENOENT).
  108. *
  109. * @param err - error
  110. * @returns true when error code contains 'ENOENT'.
  111. */
  112. function isNofileError(err: Error): boolean {
  113. return err.toString().includes('ENOENT')
  114. }
  115. /**
  116. * Get the status code by error.
  117. *
  118. * @param err - error
  119. * @returns HTTP status code
  120. */
  121. export function getHttpStatusCodeByError(err: Error): number {
  122. if (isNofileError(err)) {
  123. return 404
  124. }
  125. if (err instanceof ExtrinsicFailedError) {
  126. return 400
  127. }
  128. if (err instanceof WebApiError) {
  129. return err.httpStatusCode
  130. }
  131. if (err instanceof CLIError) {
  132. return 400
  133. }
  134. if (err instanceof BagIdValidationError) {
  135. return 400
  136. }
  137. return 500
  138. }