-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #917 from thunderstore-io/continuing-908
Fix CreateTeam forms UI problems and add field error parsing to Cyberstorm forms
- Loading branch information
Showing
8 changed files
with
63 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,61 @@ | ||
type JSONValue = | ||
| string | ||
| number | ||
| boolean | ||
| { [x: string]: JSONValue } | ||
| JSONValue[]; | ||
|
||
export function isApiError(e: Error | ApiError | unknown): e is ApiError { | ||
return e instanceof ApiError; | ||
} | ||
|
||
export class ApiError extends Error { | ||
constructor(message?: string) { | ||
super(message); | ||
response: Response; | ||
responseJson?: JSONValue; | ||
|
||
constructor(args: { | ||
message: string; | ||
response: Response; | ||
responseJson?: JSONValue; | ||
}) { | ||
super(args.message); | ||
this.responseJson = args.responseJson; | ||
this.response = args.response; | ||
} | ||
|
||
static createFromResponse(response: Response): ApiError { | ||
// TODO: Implement response parsing for known error scenarios | ||
return new ApiError(`${response.status}: ${response.statusText}`); | ||
static async createFromResponse(response: Response): Promise<ApiError> { | ||
let responseJson: JSONValue | undefined; | ||
try { | ||
responseJson = await response.json(); | ||
} catch (e) { | ||
responseJson = undefined; | ||
} | ||
|
||
return new ApiError({ | ||
message: `${response.status}: ${response.statusText}`, | ||
response: response, | ||
responseJson: responseJson, | ||
}); | ||
} | ||
|
||
getFieldErrors(): { [key: string]: string[] } { | ||
// TODO: Implement | ||
return {}; | ||
getFieldErrors(): { [key: string | "root"]: string[] } { | ||
if (typeof this.responseJson !== "object") | ||
return { root: ["Unknown error occurred"] }; | ||
if (Array.isArray(this.responseJson)) { | ||
return { | ||
root: this.responseJson.map((x) => x.toString()), | ||
}; | ||
} else { | ||
return Object.fromEntries( | ||
Object.entries(this.responseJson).map(([key, val]) => { | ||
return [ | ||
key, | ||
Array.isArray(val) | ||
? val.map((x) => x.toString()) | ||
: [val.toString()], | ||
]; | ||
}) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters