| @@ -298,11 +298,9 @@ | ||
| 298 | 298 | throw new \Exception( 'not_connected' ); |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | $result = $app->get_image_prompt_enhanced( $data['prompt'] ); |
| 302 | - if ( is_wp_error( $result ) ) { | |
| 303 | - throw new \Exception( $result->get_error_message() ); | |
| 304 | - } | |
| 302 | + $this->throw_on_error( $result ); | |
| 305 | 303 | |
| 306 | 304 | return [ |
| 307 | 305 | 'text' => $result['text'], |
| 308 | 306 | 'response_id' => $result['responseId'], |
| @@ -327,11 +325,9 @@ | ||
| 327 | 325 | |
| 328 | 326 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| 329 | 327 | |
| 330 | 328 | $result = $app->get_completion_text( $data['payload']['prompt'], $context, $request_ids ); |
| 331 | - if ( is_wp_error( $result ) ) { | |
| 332 | - throw new \Exception( $result->get_error_message() ); | |
| 333 | - } | |
| 329 | + $this->throw_on_error( $result ); | |
| 334 | 330 | |
| 335 | 331 | return [ |
| 336 | 332 | 'text' => $result['text'], |
| 337 | 333 | 'response_id' => $result['responseId'], |
| @@ -380,11 +376,9 @@ | ||
| 380 | 376 | |
| 381 | 377 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| 382 | 378 | |
| 383 | 379 | $result = $app->get_edit_text( $data, $context, $request_ids ); |
| 384 | - if ( is_wp_error( $result ) ) { | |
| 385 | - throw new \Exception( $result->get_error_message() ); | |
| 386 | - } | |
| 380 | + $this->throw_on_error( $result ); | |
| 387 | 381 | |
| 388 | 382 | return [ |
| 389 | 383 | 'text' => $result['text'], |
| 390 | 384 | 'response_id' => $result['responseId'], |
| @@ -411,11 +405,9 @@ | ||
| 411 | 405 | |
| 412 | 406 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| 413 | 407 | |
| 414 | 408 | $result = $app->get_custom_code( $data, $context, $request_ids ); |
| 415 | - if ( is_wp_error( $result ) ) { | |
| 416 | - throw new \Exception( $result->get_error_message() ); | |
| 417 | - } | |
| 409 | + $this->throw_on_error( $result ); | |
| 418 | 410 | |
| 419 | 411 | return [ |
| 420 | 412 | 'text' => $result['text'], |
| 421 | 413 | 'response_id' => $result['responseId'], |
| @@ -447,11 +439,9 @@ | ||
| 447 | 439 | $context = $this->get_request_context( $data ); |
| 448 | 440 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| 449 | 441 | |
| 450 | 442 | $result = $app->get_custom_css( $data, $context, $request_ids ); |
| 451 | - if ( is_wp_error( $result ) ) { | |
| 452 | - throw new \Exception( $result->get_error_message() ); | |
| 453 | - } | |
| 443 | + $this->throw_on_error( $result ); | |
| 454 | 444 | |
| 455 | 445 | return [ |
| 456 | 446 | 'text' => $result['text'], |
| 457 | 447 | 'response_id' => $result['responseId'], |
| @@ -502,11 +492,9 @@ | ||
| 502 | 492 | $request_ids = $this->get_request_ids( $data['payload'] ); |
| 503 | 493 | |
| 504 | 494 | $result = $app->get_text_to_image( $data, $context, $request_ids ); |
| 505 | 495 | |
| 506 | - if ( is_wp_error( $result ) ) { | |
| 507 | - throw new \Exception( $result->get_error_message() ); | |
| 508 | - } | |
| 496 | + $this->throw_on_error( $result ); | |
| 509 | 497 | |
| 510 | 498 | return [ |
| 511 | 499 | 'images' => $result['images'], |
| 512 | 500 | 'response_id' => $result['responseId'], |
| @@ -543,11 +531,9 @@ | ||
| 543 | 531 | 'promptSettings' => $data['payload']['settings'], |
| 544 | 532 | 'attachment_id' => $data['payload']['image']['id'], |
| 545 | 533 | ], $context, $request_ids ); |
| 546 | 534 | |
| 547 | - if ( is_wp_error( $result ) ) { | |
| 548 | - throw new \Exception( $result->get_error_message() ); | |
| 549 | - } | |
| 535 | + $this->throw_on_error( $result ); | |
| 550 | 536 | |
| 551 | 537 | return [ |
| 552 | 538 | 'images' => $result['images'], |
| 553 | 539 | 'response_id' => $result['responseId'], |
| @@ -579,11 +565,9 @@ | ||
| 579 | 565 | 'promptSettings' => $data['payload']['promptSettings'], |
| 580 | 566 | 'attachment_id' => $data['payload']['image']['id'], |
| 581 | 567 | ], $context, $request_ids ); |
| 582 | 568 | |
| 583 | - if ( is_wp_error( $result ) ) { | |
| 584 | - throw new \Exception( $result->get_error_message() ); | |
| 585 | - } | |
| 569 | + $this->throw_on_error( $result ); | |
| 586 | 570 | |
| 587 | 571 | return [ |
| 588 | 572 | 'images' => $result['images'], |
| 589 | 573 | 'response_id' => $result['responseId'], |
| @@ -615,11 +599,9 @@ | ||
| 615 | 599 | 'attachment_id' => $data['payload']['image']['id'], |
| 616 | 600 | 'prompt' => $data['payload']['prompt'], |
| 617 | 601 | ], $context, $request_ids ); |
| 618 | 602 | |
| 619 | - if ( is_wp_error( $result ) ) { | |
| 620 | - throw new \Exception( $result->get_error_message() ); | |
| 621 | - } | |
| 603 | + $this->throw_on_error( $result ); | |
| 622 | 604 | |
| 623 | 605 | return [ |
| 624 | 606 | 'images' => $result['images'], |
| 625 | 607 | 'response_id' => $result['responseId'], |
| @@ -645,11 +627,9 @@ | ||
| 645 | 627 | $result = $app->get_image_to_image_remove_background( [ |
| 646 | 628 | 'attachment_id' => $data['payload']['image']['id'], |
| 647 | 629 | ], $context, $request_ids ); |
| 648 | 630 | |
| 649 | - if ( is_wp_error( $result ) ) { | |
| 650 | - throw new \Exception( $result->get_error_message() ); | |
| 651 | - } | |
| 631 | + $this->throw_on_error( $result ); | |
| 652 | 632 | |
| 653 | 633 | return [ |
| 654 | 634 | 'images' => $result['images'], |
| 655 | 635 | 'response_id' => $result['responseId'], |
| @@ -691,11 +671,9 @@ | ||
| 691 | 671 | 'attachment_id' => $data['payload']['image']['id'], |
| 692 | 672 | 'mask' => $data['payload']['mask'], |
| 693 | 673 | ], $context, $request_ids ); |
| 694 | 674 | |
| 695 | - if ( is_wp_error( $result ) ) { | |
| 696 | - throw new \Exception( $result->get_error_message() ); | |
| 697 | - } | |
| 675 | + $this->throw_on_error( $result ); | |
| 698 | 676 | |
| 699 | 677 | return [ |
| 700 | 678 | 'images' => $result['images'], |
| 701 | 679 | 'response_id' => $result['responseId'], |
| @@ -725,11 +703,9 @@ | ||
| 725 | 703 | 'prompt' => $data['payload']['prompt'], |
| 726 | 704 | 'mask' => $data['payload']['mask'], |
| 727 | 705 | ], $context, $request_ids ); |
| 728 | 706 | |
| 729 | - if ( is_wp_error( $result ) ) { | |
| 730 | - throw new \Exception( $result->get_error_message() ); | |
| 731 | - } | |
| 707 | + $this->throw_on_error( $result ); | |
| 732 | 708 | |
| 733 | 709 | return [ |
| 734 | 710 | 'images' => $result['images'], |
| 735 | 711 | 'response_id' => $result['responseId'], |
| @@ -786,11 +762,12 @@ | ||
| 786 | 762 | $message = $result->get_error_message(); |
| 787 | 763 | |
| 788 | 764 | if ( is_array( $message ) ) { |
| 789 | 765 | $message = implode( ', ', $message ); |
| 766 | + throw new \Exception( $message ); | |
| 790 | 767 | } |
| 791 | 768 | |
| 792 | - throw new \Exception( $message ); | |
| 769 | + $this->throw_on_error( $result ); | |
| 793 | 770 | } |
| 794 | 771 | |
| 795 | 772 | $elements = $result['text']['elements'] ?? []; |
| 796 | 773 | $base_template_id = $result['baseTemplateId'] ?? null; |
| @@ -853,11 +830,9 @@ | ||
| 853 | 830 | $data['enhance_type'], |
| 854 | 831 | $this->prepare_generate_layout_context() |
| 855 | 832 | ); |
| 856 | 833 | |
| 857 | - if ( is_wp_error( $result ) ) { | |
| 858 | - throw new \Exception( $result->get_error_message() ); | |
| 859 | - } | |
| 834 | + $this->throw_on_error( $result ); | |
| 860 | 835 | |
| 861 | 836 | return [ |
| 862 | 837 | 'text' => $result['text'] ?? $data['prompt'], |
| 863 | 838 | 'response_id' => $result['responseId'] ?? '', |
| @@ -1019,6 +994,18 @@ | ||
| 1019 | 994 | throw new \Exception( $result->get_error_message() ); |
| 1020 | 995 | } |
| 1021 | 996 | |
| 1022 | 997 | return []; |
| 998 | + } | |
| 999 | + | |
| 1000 | + /** | |
| 1001 | + * @param mixed $result | |
| 1002 | + */ | |
| 1003 | + private function throw_on_error( $result ): void { | |
| 1004 | + if ( is_wp_error( $result ) ) { | |
| 1005 | + wp_send_json_error( [ | |
| 1006 | + 'message' => $result->get_error_message(), | |
| 1007 | + 'extra_data' => $result->get_error_data(), | |
| 1008 | + ] ); | |
| 1009 | + } | |
| 1023 | 1010 | } |
| 1024 | 1011 | } |