Skip to content

Commit

Permalink
Response class is missing status code property #3218
Browse files Browse the repository at this point in the history
  • Loading branch information
alansemenov committed Aug 10, 2023
1 parent ca0b4c8 commit 9d1b7aa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export class JsonResponse<RAW_JSON_TYPE>

readonly json: any;

constructor(data: any) {
super(data);
constructor(data: any, status?: number) {
super(data, status);
this.json = JSON.parse(data);
}

Expand Down
12 changes: 8 additions & 4 deletions src/main/resources/assets/admin/common/js/rest/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Path} from './Path';
import {RequestError} from './RequestError';
import {HttpMethod} from './HttpMethod';
import {Response} from './Response';
import {StatusCode} from './StatusCode';

export abstract class Request {

Expand Down Expand Up @@ -43,11 +44,11 @@ export abstract class Request {
if (this.request.readyState === 4) {
let errorJson = null;

if (this.request.status === 204) {
if (this.request.status === StatusCode.NO_CONTENT) {
deferred.resolve(null);
} else if (this.request.status >= 200 && this.request.status < 300) {
} else if (this.request.status >= StatusCode.OK && this.request.status < StatusCode.MULTIPLE_OPTIONS) {
deferred.resolve(this.request.response);
} else if (this.request.status === 403) {
} else if (this.request.status === StatusCode.FORBIDDEN) {
deferred.reject(new AccessDeniedException('Access denied'));
} else {
try {
Expand All @@ -73,11 +74,14 @@ export abstract class Request {
}

send(): Q.Promise<Response> {

this.prepareRequest();
return this.sendRequest();
}

getStatus(): number {
return this.request.status;
}

protected createRequestData(): any {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export abstract class ResourceRequest<PARSED_TYPE>
.setTimeout(!this.heavyOperation ? this.timeoutMillis : 0);

return request.send().then((rawResponse: any) => {
return this.isJsonResponse ? new JsonResponse(rawResponse) : new Response(rawResponse);
return this.isJsonResponse ? new JsonResponse(rawResponse, request.getStatus()) : new Response(rawResponse, request.getStatus());
});
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/assets/admin/common/js/rest/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ export class Response {

readonly data: any;

constructor(data: any) {
readonly status: number;

constructor(data: any, status?: number) {
this.data = data;
this.status = status;
}

isBlank(): boolean {
Expand All @@ -17,4 +20,8 @@ export class Response {
getResult(): any {
return this.data;
}

getStatus(): number {
return this.status;
}
}
8 changes: 8 additions & 0 deletions src/main/resources/assets/admin/common/js/rest/StatusCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export class StatusCode {

public static OK: number = 200;
public static NO_CONTENT: number = 204;

public static MULTIPLE_OPTIONS: number = 300;
public static REDIRECT: number = 307;

public static FORBIDDEN: number = 403;
public static NOT_FOUND: number = 404;
public static I_AM_A_TEAPOT: number = 418;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {UploadFailedEvent} from './UploadFailedEvent';
import {KeyBindings} from '../KeyBindings';
import {KeyBinding} from '../KeyBinding';
import {KeyHelper} from '../KeyHelper';
import {StatusCode} from '../../rest/StatusCode';

export interface FineUploaderFile {
id: number;
Expand Down Expand Up @@ -858,7 +859,7 @@ export class UploaderEl<MODEL extends Equitable>
}

private fileCompleteCallback(id: number, _name: string, response: any, xhrOrXdr: XMLHttpRequest) {
if (xhrOrXdr && xhrOrXdr.status === 200) {
if (xhrOrXdr && xhrOrXdr.status === StatusCode.OK) {
try {
const uploadItem = this.findUploadItemById(id);
if (uploadItem) {
Expand All @@ -878,7 +879,7 @@ export class UploaderEl<MODEL extends Equitable>
}

private errorCallback(id: number, name: string, _errorReason: any, xhrOrXdr: XMLHttpRequest) {
if (xhrOrXdr && xhrOrXdr.status !== 200) {
if (xhrOrXdr && xhrOrXdr.status !== StatusCode.OK) {
try {
const responseObj = JSON.parse(xhrOrXdr.response);
const error = new RequestError(responseObj.status, responseObj.message);
Expand Down

0 comments on commit 9d1b7aa

Please sign in to comment.