Skip to content

Witchcraft error catching #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ import * as React from "react";
import { Router, Route, IndexRoute, hashHistory } from "react-router";
import Layout from "./Layout";
import Project from "./views/Project";
import SignIn from "./views/SignIn";
import Home from "./views/Home";
import {map, actionsInterface} from "./actions";

export interface AppProps { title: string; }
export interface AppState { open?: boolean; title?: string; }

export default () => (
<Router history={hashHistory} >
<Route path="/" component={Layout}>
<IndexRoute component={Home} />
<Route path="project/:id" component={Project} />
</Route>
</Router>
);
class App extends React.Component<AppProps&actionsInterface, AppState> {
render() {
const projects = this.props.appState.projects;
return this.props.user.questid == null ? <SignIn/> : (
<Router history={hashHistory} >
<Route path="/" component={Layout}>
<IndexRoute component={Home} />
<Route path="project/:id" component={Project} />
</Route>
</Router>);
}
}

export default map(App);
29 changes: 28 additions & 1 deletion src/frontend/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { ComponentClass } from "react";
import { globalState } from "../reducers/";
import {projectRef, fileRef} from "../types";
import {appStateActions} from "../reducers/appStateReducer";
import {userActions} from "../reducers/userReducer";
import { Services, GenericError, LoginError } from "../helpers/Services";
import { showError } from "../partials/Errors";
import { trim } from "ramda";
import {settingsActions, settingsReducerState} from "../reducers/settingsReducer";
import {Services} from "../helpers/Services";
import {File} from "../helpers/Storage/Interface";


Expand All @@ -19,6 +22,20 @@ function returnType<T>(func: Func<T>) {
const mapStoreToProps = (state: globalState) => state;

const mapDispatchToProps = (dispatch: Function) => {

async function asyncAction<T>(pr: Promise<T>) {
try {
return await pr;
}catch (e) {
if (e instanceof LoginError) {
showError(e.message);
dispatch({type: userActions.INVALIDATE});
} else {
throw e;
}
}
}

return {
dispatch: {
settings: {
Expand Down Expand Up @@ -65,6 +82,16 @@ const mapDispatchToProps = (dispatch: Function) => {
Services.storage().getFiles(project).then((files) => dispatch({type: appStateActions.switchQuestion, payload: {question: {name: name, files: files.filter((file) => file.name.split("/")[0] === name).map((file) => file.name)}}}));
}
},
user: {
signin: (username: string, password: string) => {
if (trim(username) === "" || trim(password) === ""){
showError("Please fill in all fields!");
} else {
let result = asyncAction(Services.login(username, password));
alert("hi!");
}
}
},
project: {
addProject: (newProjectName: string) => Services.storage().newProject(newProjectName).then(() => dispatch({type: appStateActions.addProject, payload: {name: newProjectName}})),
removeProject: (name: string) => Services.storage().deleteProject(name).then(() => dispatch({type: appStateActions.removeProject, payload: {name: name}})),
Expand Down
16 changes: 12 additions & 4 deletions src/frontend/src/helpers/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ import {AbstractCompiler,

export * from "./Storage/Interface";
export * from "./Compiler/Interface";
export {Services, LoginError, Connection, DispatchFunction};
export {Services, GenericError, LoginError, Connection, DispatchFunction};

class LoginError extends Error {
constructor(msg: string,
class GenericError extends Error {
__proto__: Error;
constructor(message?: string) {
const trueProto = new.target.prototype;
super(message);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what's the point of this class? It just sets __proto__ to Error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fix to repair the prototype chain so instanceof works properly. See #721.

class LoginError extends GenericError {
constructor(message: string,
public username?: string,
public status?: number,
public statusText?: number) {
super(msg);
super(message);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/partials/Errors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const Errors = Toaster.create({
position: Position.BOTTOM,
});

export default (message: string) => Errors.show({ message: message, timeout: 0, intent: Intent.DANGER });

export const showError = (message: string)=>Errors.show({ message: message, timeout: 0, intent: Intent.DANGER });;
2 changes: 1 addition & 1 deletion src/frontend/src/partials/Navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SettingsDialog from "./Dialogs";
import * as R from "ramda";


const logo = require("./logo.svg");
const logo = require("../../assets/logo.svg");
const styles = require("./index.scss");
const layoutStyles = require("../../Layout.scss");

Expand Down
2 changes: 0 additions & 2 deletions src/frontend/src/reducers/appStateReducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {mergeBetter} from "../helpers/utils";
import {clone, reject, equals} from "ramda";
import Error from "../partials/Errors";
import {projectRef, fileRef} from "../types";
export interface appStateReducerState {[key: string]: any;
projects: string[];
Expand Down Expand Up @@ -76,7 +75,6 @@ export default function appStateReducer(state: appStateReducerState = {currentPr
case appStateActions.changeFileContent:
return mergeBetter(state, {currentProject: { currentQuestion: {currentFile: {content: action.payload}}}});
case appStateActions.openFile:
Error("You opened a file");
if (state.currentProject.currentQuestion.openFiles.indexOf(action.payload) !== -1) return state; // don't duplicate files
state = clone(state);
state.currentProject.currentQuestion.openFiles.push(action.payload);
Expand Down
10 changes: 8 additions & 2 deletions src/frontend/src/reducers/userReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ export interface userReducerState {[key: string]: any;
questid?: string;
};
export interface userReducerAction {type: string; payload: userReducerState; };
export const appStateActions = {
export const userActions = {
INVALIDATE: "USER_INVALIDATE",
SIGNIN: "USER_SIGNIN"
};
export default function assignmentReducer(state: userReducerState = {questid: "kcpei"}, action: userReducerAction= {type: null, payload: {}}) {
export default function userReducer(state: userReducerState = {questid: null}, action: userReducerAction= {type: null, payload: {}}) {
switch (action.type) {
case userActions.INVALIDATE:
return {questid: null};
case userActions.SIGNIN:
return {questid: action.payload};
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/views/Project/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import DeleteWindow from "./DeleteWindow";


export interface ProjectProps { title: string; routeParams: any; }
export interface ProjectState { deleteVisible: boolean; renameVisible: boolean; copyVisible: boolean; fileOpTarget: string; toggleView: boolean}
export interface ProjectState { deleteVisible: boolean; renameVisible: boolean; copyVisible: boolean; fileOpTarget: string; toggleView: boolean; }



Expand Down
15 changes: 15 additions & 0 deletions src/frontend/src/views/SignIn.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.container{
max-width:300px;
margin:0 auto;
padding:4em 1em;
text-align:center;
h5{
margin-bottom:2em;
}
}
.logo{
width:50%;
max-width:80px;
display:block;
margin:0 auto 3em auto;
}
40 changes: 40 additions & 0 deletions src/frontend/src/views/SignIn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from "react";
import {map, actionsInterface} from "../actions";
const layoutStyles = require("../Layout.scss");
const styles = require("./SignIn.scss");

const logo = require("../assets/logo.svg");

export interface SignInProps { }
export interface SignInState { }

class SignIn extends React.Component<SignInProps&actionsInterface, SignInState> {
constructor(props: SignInProps){
super(props);
this.signin = this.signin.bind(this);
}
signin(e: any) {
e.preventDefault();
this.props.dispatch.user.signin((this.refs.username as HTMLInputElement).value, (this.refs.password as HTMLInputElement).value);
}
render() {
const projects = this.props.appState.projects;
return (<div className={styles.container}>
<img src={logo} className={styles.logo}/><h5>Sign in to Seashell</h5>
<form className="pt-control-group pt-vertical" onSubmit={this.signin}>
<div className="pt-input-group pt-large">
<span className="pt-icon pt-icon-person"></span>
<input type="text" className="pt-input" ref="username" placeholder="Username" />
</div>
<div className="pt-input-group pt-large">
<span className="pt-icon pt-icon-lock"></span>
<input type="password" className="pt-input" ref="password" placeholder="Password" />
</div>
<button className="pt-button pt-large pt-intent-primary">Sign In</button>
</form>

</div>);
}
}

export default map(SignIn);