Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
console
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
console
Commits
5cdac6e6
Commit
5cdac6e6
authored
Jan 05, 2017
by
wudizhanche1000
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
将错误用Error对象传到前端
parent
db696688
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
15 deletions
+44
-15
app/app.service.ts
app/app.service.ts
+17
-2
koa/errors.ts
koa/errors.ts
+5
-1
models/app.ts
models/app.ts
+1
-1
models/errors.ts
models/errors.ts
+13
-6
routes/apps.ts
routes/apps.ts
+4
-4
server.ts
server.ts
+4
-1
No files found.
app/app.service.ts
View file @
5cdac6e6
import
{
Injectable
}
from
'
@angular/core
'
;
import
App
from
'
../models/browserapp
'
;
import
{
Http
,
RequestOptions
,
Headers
}
from
'
@angular/http
'
;
import
{
Http
,
RequestOptions
,
Headers
,
Response
}
from
'
@angular/http
'
;
import
'
rxjs/add/operator/toPromise
'
;
import
{
Observable
}
from
'
rxjs/Rx
'
;
/**
* Created by weijian on 2016/12/30.
*/
...
...
@@ -19,10 +20,24 @@ export class AppService {
return
this
.
http
.
get
(
`http://localhost:8000/apps/
${
id
}
`
).
map
((
response
)
=>
new
App
(
response
.
json
())).
toPromise
();
}
handleError
(
error
:
Response
|
any
)
{
// In a real world app, we might use a remote logging infrastructure
let
err
:
Error
;
if
(
error
instanceof
Response
)
{
err
=
error
.
json
()
||
{};
}
else
{
err
=
new
Error
(
'
Unknown Error
'
);
}
return
Observable
.
throw
(
err
);
}
save
(
app
:
App
):
Promise
<
any
>
{
let
headers
=
new
Headers
({
'
Content-Type
'
:
'
application/json
'
});
let
options
=
new
RequestOptions
({
headers
:
headers
});
return
this
.
http
.
post
(
`http://localhost:8000/apps/
${
app
.
id
}
`
,
app
,
options
).
map
((
response
)
=>
response
.
json
()).
toPromise
();
return
this
.
http
.
post
(
`http://localhost:8000/apps/
${
app
.
id
}
`
,
app
,
options
)
.
map
((
response
)
=>
response
.
json
())
.
catch
(
this
.
handleError
)
.
toPromise
();
}
update
(
app
:
App
)
{
...
...
koa/errors.ts
View file @
5cdac6e6
...
...
@@ -19,5 +19,9 @@ export class BadRequest extends KoaError {
super
(
msg
,
400
);
}
}
export
const
InternalError
=
new
KoaError
(
STATUS_CODES
[
500
],
500
);
export
class
InternalError
extends
KoaError
{
constructor
(
public
error
:
Error
)
{
super
(
STATUS_CODES
[
500
],
500
);
}
}
models/app.ts
View file @
5cdac6e6
...
...
@@ -28,7 +28,7 @@ export class App extends Model {
async
checkExists
()
{
let
app
=
await
App
.
findOne
({
id
:
this
.
id
});
if
(
app
)
{
throw
new
ModelExistsError
(
this
.
id
);
throw
new
ModelExistsError
(
`App
${
this
.
id
}
exists`
);
}
}
...
...
models/errors.ts
View file @
5cdac6e6
import
{
KoaError
}
from
'
../koa/errors
'
;
/**
* Created by weijian on 2017/1/4.
*/
class
ModelError
extends
Error
{
constructor
(
msg
:
string
)
{
super
(
msg
);
export
abstract
class
ModelError
extends
KoaError
{
abstract
errCode
:
string
;
constructor
(
msg
:
string
,
status
:
number
)
{
super
(
msg
,
status
);
}
}
export
class
ModelExistsError
extends
Error
{
constructor
(
id
:
string
)
{
super
(
`App
${
id
}
already exists`
);
export
class
ModelExistsError
extends
ModelError
{
errCode
:
string
=
'
ERROR_MODULE_EXISTS
'
;
constructor
(
msg
:
string
)
{
super
(
msg
,
400
);
}
}
routes/apps.ts
View file @
5cdac6e6
...
...
@@ -5,7 +5,7 @@ import Router = require('koa-router');
import
{
NotFound
,
BadRequest
,
InternalError
}
from
'
../koa/errors
'
;
import
{
Model
}
from
'
../db/mongo
'
;
import
{
App
}
from
'
../models/app
'
;
import
{
ModelExistsError
}
from
'
../models/errors
'
;
import
{
ModelExistsError
,
ModelError
}
from
'
../models/errors
'
;
const
router
=
new
Router
();
router
.
get
(
'
/apps
'
,
async
(
ctx
,
next
)
=>
{
...
...
@@ -26,10 +26,10 @@ router.post('/apps/:id', async(ctx, next) => {
try
{
ctx
.
body
=
await
app
.
save
();
}
catch
(
e
)
{
if
(
e
instanceof
ModelE
xistsE
rror
)
{
throw
new
BadRequest
(
`App
${
ctx
.
params
.
id
}
already exists`
)
;
if
(
e
instanceof
ModelError
)
{
throw
e
;
}
else
{
throw
InternalError
;
throw
new
InternalError
(
e
)
;
}
}
});
...
...
server.ts
View file @
5cdac6e6
...
...
@@ -28,8 +28,11 @@ app.use(async(ctx, next) => {
// will only respond with JSON
ctx
.
status
=
err
.
status
||
500
;
ctx
.
body
=
{
message
:
err
.
message
message
:
err
.
message
,
};
if
(
err
.
errCode
)
{
ctx
.
body
[
'
errCode
'
]
=
err
.
errCode
;
}
if
(
ctx
.
response
.
status
>=
400
)
{
logger
.
warn
(
err
);
}
else
if
(
ctx
.
response
.
status
>=
500
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment