Skip to content

Commit

Permalink
refactor(http): tree-shakable error on JSONP request (#50376)
Browse files Browse the repository at this point in the history
This commit provides a tree shakable error message (+doc) when JSONP request is made without loading the `HttpClientJsonpModule`.

PR Close #50376
  • Loading branch information
JeanMeche authored and thePunderWoman committed May 24, 2023
1 parent 6e4f582 commit f3a13c2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
23 changes: 23 additions & 0 deletions aio/content/errors/NG02800.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@name JSONP support in HttpClient configuration
@category runtime
@shortDescription Missing JSONP support in HttpClient configuration

@description
Angular produces this error when you attempt a `JSONP` request without providing the necessary support for it in the `HttpClient` configuration.
To enable `JSONP` support, you can do one of the following:
- add the `withJsonpSupport()` as an argument during the `provideHttpClient` function call (e.g. `provideHttpClient(withJsonpSupport())`) when `bootstrapApplication` is used
- import the `HttpClientJsonpModule` in your root AppModule, when NgModule-based bootstrap is used.


@debugging
Make sure that the JSONP support is added into your application either by calling the `withJsonpSupport()` function (when the `provideHttpClient()` is used) or importing the `HttpClientJsonpModule` module as described above.

See [Make a JSONP request](/guide/http-make-jsonp-request) for more info.

<!-- links -->

<!-- external links -->

<!-- end links -->

@reviewed 2023-05-02
15 changes: 15 additions & 0 deletions goldens/public-api/common/http/errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## API Report File for "angular-srcs"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

// @public
export const enum RuntimeErrorCode {
// (undocumented)
MISSING_JSONP_MODULE = -2800
}

// (No @packageDocumentation comment for this package)

```
12 changes: 11 additions & 1 deletion packages/common/http/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools:defaults.bzl", "ng_module")
load("//tools:defaults.bzl", "api_golden_test", "ng_module")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -26,3 +26,13 @@ filegroup(
"src/**/*.ts",
]) + ["PACKAGE.md"],
)

api_golden_test(
name = "http_errors",
data = [
"//goldens:public-api",
"//packages/common/http",
],
entry_point = "angular/packages/common/http/src/errors.d.ts",
golden = "angular/goldens/public-api/common/http/errors.md",
)
15 changes: 15 additions & 0 deletions packages/common/http/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

/**
* The list of error codes used in runtime code of the `common/http` package.
* Reserved error code range: 2800-2899.
*/
export const enum RuntimeErrorCode {
MISSING_JSONP_MODULE = -2800,
}
9 changes: 6 additions & 3 deletions packages/common/http/src/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/

import {XhrFactory} from '@angular/common';
import {Injectable} from '@angular/core';
import {Injectable, ɵRuntimeError as RuntimeError} from '@angular/core';
import {from, Observable, Observer, of} from 'rxjs';
import {switchMap} from 'rxjs/operators';

import {HttpBackend} from './backend';
import {RuntimeErrorCode} from './errors';
import {HttpHeaders} from './headers';
import {HttpRequest} from './request';
import {HttpDownloadProgressEvent, HttpErrorResponse, HttpEvent, HttpEventType, HttpHeaderResponse, HttpJsonParseError, HttpResponse, HttpStatusCode, HttpUploadProgressEvent} from './response';
Expand Down Expand Up @@ -53,8 +54,10 @@ export class HttpXhrBackend implements HttpBackend {
// Quick check to give a better error message when a user attempts to use
// HttpClient.jsonp() without installing the HttpClientJsonpModule
if (req.method === 'JSONP') {
throw new Error(
`Attempted to construct Jsonp request without HttpClientJsonpModule installed.`);
throw new RuntimeError(
RuntimeErrorCode.MISSING_JSONP_MODULE,
(typeof ngDevMode === 'undefined' || ngDevMode) &&
`Cannot make a JSONP request without JSONP support. To fix the problem, either add the \`withJsonpSupport()\` call (if \`provideHttpClient()\` is used) or import the \`HttpClientJsonpModule\` in the root NgModule.`);
}

// Check whether this factory has a special function to load an XHR implementation
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const enum RuntimeErrorCode {
// NgForOf errors
NG_FOR_MISSING_DIFFER = -2200,

// Keep 2800 - 2900 for Http Errors.

// Image directive errors
UNEXPECTED_SRC_ATTR = 2950,
UNEXPECTED_SRCSET_ATTR = 2951,
Expand Down

0 comments on commit f3a13c2

Please sign in to comment.