not ok throw Error

This commit is contained in:
monoid 2021-01-10 19:05:13 +09:00
parent be60b5a602
commit dea548cd4a

View File

@ -3,16 +3,27 @@ import {toQueryString} from './util';
const baseurl = "/content"; const baseurl = "/content";
export * from "../../model/contents"; export * from "../../model/contents";
export class FetchFailError implements Error{
name: string;
message: string;
stack?: string;
constructor(message:string,stack?:string){
this.name = "Fetch Fail";
this.message = message;
this.stack = stack;
}
}
export class ClientContentAccessor implements ContentAccessor{ export class ClientContentAccessor implements ContentAccessor{
async findList(option?: QueryListOption | undefined): Promise<Content[]>{ async findList(option?: QueryListOption | undefined): Promise<Content[]>{
let res = await fetch(`${baseurl}/search?${option !== undefined ? toQueryString(option) : ""}`); let res = await fetch(`${baseurl}/search?${option !== undefined ? toQueryString(option) : ""}`);
//if(res.status !== 200); if(res.status !== 200) throw new FetchFailError("findList Failed");
let ret = await res.json(); let ret = await res.json();
return ret; return ret;
} }
async findById(id: number, tagload?: boolean | undefined): Promise<Content | undefined>{ async findById(id: number, tagload?: boolean | undefined): Promise<Content | undefined>{
let res = await fetch(`${baseurl}/${id}`); let res = await fetch(`${baseurl}/${id}`);
if(res.status !== 200) return undefined; if(res.status !== 200) throw new FetchFailError("findById Failed");;
let ret = await res.json(); let ret = await res.json();
return ret; return ret;
} }