How to use the http-status-codes.BAD_REQUEST function in http-status-codes

To help you get started, we’ve selected a few http-status-codes examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Viveckh / Veniqa / shopping-server / services / userService.js View on Github external
let index = _.findIndex(user.addresses, (obj) => {
                return obj._id.toString() == addressObj._id;
            });

            // If the address is not found, return failure
            if (index == -1) {
                result = {httpStatus: httpStatus.NOT_FOUND, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.NOT_FOUND)};
                return result;
            }

            // Otherwise, Update the address values
            user.addresses[index] = addressObj;
            user = await user.save();

            // Return the proper response depending on whether save was successful
            result = user ? {httpStatus: httpStatus.OK, status: "successful", responseData: user.addresses} : {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: httpStatus.getStatusText(httpStatus.BAD_REQUEST)};
            return result;
        }
        catch(err) {
            logger.error("Error in updateAddress Service", {meta: err});
            result = {httpStatus: httpStatus.BAD_REQUEST, status: "failed", errorDetails: err};
            return result;
        }
    },
github adobe / commerce-cif-magento / test / carts / deleteCartEntryIT.js View on Github external
.then(function(res) {
                    expect(res).to.have.status(HttpStatus.BAD_REQUEST);
                    expect(res).to.be.json;
                    requiredFields.verifyErrorResponse(res.body);
                });
        });
github mozilla / voice-web / server / src / server.ts View on Github external
(
        error: Error,
        request: Request,
        response: Response,
        next: NextFunction
      ) => {
        console.log(error.message, error.stack);
        const isAPIError = error instanceof APIError;
        if (!isAPIError) {
          console.error(request.url, error.message, error.stack);
        }
        response
          .status(
            error instanceof ClientError
              ? HttpStatus.BAD_REQUEST
              : HttpStatus.INTERNAL_SERVER_ERROR
          )
          .json({ message: isAPIError ? error.message : '' });
      }
    );
github jungho / k8s-bootcamp / todo-app / user-api / src / controller.js View on Github external
router.post('/', authMiddleware, async ({user: {email, firstName, lastName}}, res) => {
    logger.info(`POST /user - creating new user where email = ${email}`);

    try {
        const existingUser = await dao.findUserByEmail(email);
        if (existingUser) {
            logger.info(`POST /user - user already exists where email = ${email}`);
            return res.sendStatus(HttpStatus.BAD_REQUEST);
        }

        const newUser = await dao.createUser({email, firstName, lastName});

        logger.info(`POST /user - user created successfully where email = ${email}`);
        res.status(HttpStatus.CREATED).send(new UserDTO(newUser));
    } catch (e) {
        logger.error(`Error creating user where email = ${email}`);
        res.sendStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    }
});
github microsoft / BotFramework-Emulator / packages / app / main / src / server / state / attachments.ts View on Github external
public uploadAttachment(attachmentData: AttachmentData): string {
    if (!attachmentData.type) {
      throw createAPIException(
        HttpStatus.BAD_REQUEST,
        ErrorCodes.MissingProperty,
        'You must specify type property for the attachment'
      );
    }

    if (!attachmentData.originalBase64) {
      throw createAPIException(
        HttpStatus.BAD_REQUEST,
        ErrorCodes.MissingProperty,
        'You must specify originalBase64 byte[] for the attachment'
      );
    }

    const attachment: any = { ...attachmentData, id: uniqueId() };

    this.attachments[attachment.id] = attachment;

    return attachment.id;
  }
}
github pietrzakadrian / bank / backend / src / controllers / create.controller.ts View on Github external
senderService.sendPaymentMail(
          user,
          language,
          currency,
          recipient,
          authorizationKey,
          amountMoney
        );

        return res.status(HttpStatus.OK).json({
          success: true
        });
      } catch (error) {
        const err: IResponseError = {
          success: false,
          code: HttpStatus.BAD_REQUEST,
          error
        };
        next(err);
      }
    }
  );
github Azure / meta-azure-service-broker / lib / services / azurestorage / storageclient.js View on Github external
function(err, response, body) {
            common.logHttpResponse(response, 'Storage - checkNameAvailability', true);
            if (err) {
              callback(err);
            } else {
              if (body.nameAvailable) {
                callback(null);
              } else {
                var error = new Error(body.message);
                if (body.reason === 'AccountNameInvalid') {
                  error.statusCode = HttpStatus.BAD_REQUEST;
                } else {
                  error.statusCode = HttpStatus.CONFLICT;
                }
                callback(error);
              }
            }
          }
        );
github microsoft / BotFramework-Emulator / src / server / controllers / connector / attachmentsController.ts View on Github external
public static uploadAttachment(attachmentData: IAttachmentData): string {
        if (!attachmentData.type)
            throw ResponseTypes.createAPIException(HttpStatus.BAD_REQUEST, ErrorCodes.MissingProperty, "You must specify type property for the attachment");

        if (!attachmentData.originalBase64)
            throw ResponseTypes.createAPIException(HttpStatus.BAD_REQUEST, ErrorCodes.MissingProperty, "You must specify originalBase64 byte[] for the attachment");

        let attachment: any = attachmentData;
        attachment.id = uniqueId();
        AttachmentsController.attachments[attachment.id] = attachment;

        return attachment.id;
    }
github 0xProject / 0x-monorepo / contracts / coordinator / src / client / index.ts View on Github external
endpoint: string,
    ): Promise {
        const requestPayload = {
            signedTransaction,
            txOrigin,
        };
        const response = await fetchAsync(`${endpoint}/v2/request_transaction?chainId=${this.chainId}`, {
            body: JSON.stringify(requestPayload),
            method: 'POST',
            headers: {
                'Content-Type': 'application/json; charset=utf-8',
            },
        });

        const isError = response.status !== HttpStatus.OK;
        const isValidationError = response.status === HttpStatus.BAD_REQUEST;
        const json = isError && !isValidationError ? undefined : await response.json();

        const result = {
            isError,
            status: response.status,
            body: isError ? undefined : json,
            error: isError ? json : undefined,
            request: requestPayload,
            coordinatorOperator: endpoint,
        };

        return result;
    }